python string position code example

Example 1: python string indexing

str = 'codegrepper'
# str[start:end:step]
#by default: start = 0, end = len(str), step = 1
print(str[:]) 		#codegrepper
print(str[::]) 		#codegrepper
print(str[5:]) 		#repper
print(str[:8]) 		#codegrep
print(str[::2]) 	#cdgepr
print(str[2:8]) 	#degrep
print(str[2:8:2]) 	#dge
#step < 0 : reverse
print(str[::-1]) 	#reppergedoc
print(str[::-3]) 	#rpgo
# str[start:end:-1]	means start from the end, go backward and stop at start
print(str[8:3:-1]) 	#pperg

Example 2: how to find the location of a character in a string in python

>>> myString = 'Position of a character'
>>> myString.find('s')
2
>>> myString.find('x')
-1

Example 3: python string indexof

s = "mouse"
animal_letter = s.find('s')
print animal_letter

Example 4: strings in python

#A string is a type of data. There are many data types. It can be manipulated.
#It can be storerd as a variable
myString = "Hello world"
#WE can print it:
print(myString)
#You can append it to arraY:
myArr = []
myArr.append(myString)
#You can find the index of a character in a string:
H = myString[0]
#You can use methods on it:
lowercase = myString.lower()
#You can convert it into a integer provided it is a numerical string
myInt = int(myString)
#So thats the basics, hope i haven't left anything out.

Example 5: how to find position of a character in a string from right sidepython

>>> s = 'hello'
>>> s.rfind('l')
3

Example 6: python string index od

str.index(sub[, start[, end]] )