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 :: searching for best k values in knn 
Python :: Panda Python - Calculating what percentage of values are true and false out of total in boolean column 
Python :: python time a task 
Python :: numpy percentile 
Python :: selenium session id python 
Python :: destructuring in for loops python 
Python :: permutation in python 
Python :: sum() python 
Python :: base64 python 
Python :: python django adding category 
Python :: merge two dict python 
Python :: python how to write array into matlab file 
Python :: python list sum 
Python :: scrape pdf out of link 
Python :: python list of dict change dicts id by position in list when moved 
Python :: ski learn decision tree 
Python :: How split() works in Python? 
Python :: geopandas dataframe to ogr layer 
Python :: python c like struct 
Python :: print index in for loop python 
Python :: bitwise operators in python 
Python :: Python NumPy stack Function Example with 1d array 
Python :: .corr() python 
Python :: convert to lwercase in df column 
Python :: :: python 
Python :: how to split strings in python 
Python :: que es una funcion en python 
Python :: trim strings python 
Python :: what is admin.tabularinline django 
Python :: NumPy flipud Syntax 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =