Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python string [::-1]

In [1]: string = "Howdy doody"

In [2]: string[::]
Out[2]: 'Howdy doody'

In [3]: string[::-1]
Out[3]: 'ydood ydwoH'

In [4]: string[0:]    
Out[4]: 'Howdy doody'

In [5]: string[0::-1]
Out[5]: 'H'     # what up with this?

In [6]: string[:len(string)]
Out[6]: 'Howdy doody'

In [7]: string[:len(string):-1]
Out[7]: ''     # what up with this too?

In [8]: string[0:len(string)]
Out[8]: 'Howdy doody'

In [9]: string[0:len(string):-1]
Out[9]: ''    # And what up here too.
Comment

string[::-1] meaning in python

A slice bracket has three 'slots': Start, end, and step.

[2:17:3] means: Start at 2, end before 17, go in steps of 3.

[17:2:-3] means: Start at 17, end before 2, go *backward* steps of 3.

If you leave slots empty, there's a default.

[:] means: The whole thing. 

[::1] means: Start at the beginning, end when it ends, walk in steps of 1 (which is the default, so you don't even need to write it).

[::-1] means: Start at the end (the minus does that for you), end when nothing's left and walk backwards by 1.
Comment

string -1 python

string = "Hello World"
#to index the final character of this string we would do
string[-1]
#this is useful when we dont know how long the string is going to be
#for example with user input
string = input("Enter a string: ")
last_character = string[-1]
Comment

PREVIOUS NEXT
Code Example
Python :: unban member using ID discord.py 
Python :: convert pine script to python online 
Python :: python vectorize 
Python :: python polymorphism 
Python :: how to get user input in python 
Python :: abstract classes in python 
Python :: dataframe change column types 
Python :: python pandas if statement 
Python :: login python code 
Python :: sort 2d list python 
Python :: python parse xml string 
Python :: how to find duplicates in pandas 
Python :: padding figures in pyplot 
Python :: graph outlier detection 
Python :: how to add element to list value in a dict python 
Python :: how to access variable of one function in another function in python 
Python :: how to create multiple variables in a loop python 
Python :: formatting strings in python 
Python :: delete function python 
Python :: how to inherit a class in python 
Python :: how to duplicate a list in python 
Python :: average python 
Python :: precedence in python 
Python :: initialize 2d array of zeros python 
Python :: python elif 
Python :: Math Module floor() Function in python 
Python :: ImportError: cannot import name 
Python :: read header of csv file python 
Python :: python sum of 10 numbers from user input 
Python :: Python NumPy column_stack Function Syntax 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =