Skip to main content

create string ,string slicing & other function

 mystr="satyam is doing b-tech in ECE" # In python index starts from 0 i.e. s is at 0 place ,a is at 1st place and soo on..

mystr1="satyamisgoodboy"
'''print(mystr)
print(len(mystr)) # it print the length of mystr
print(mystr[4]) # It only print the latter at 4th place
print(mystr[2]) # It only print latter at 2nd place
print(mystr[0:6]) # It print letter from 0th se 5th place
print(mystr[10:30]) # It print letter from 10th to 29th place
print(mystr[0:6:2]) # here :2 indicates that it print letter from 0th se 5th place with one gap
print(mystr[0:6:3]) # here :3 indicates that it print letter from 0th se 5th place with two gap
print(mystr[:5]) # here it assume that it start from 0
print(mystr[0:]) # here it assume that it ends at length of mystr
print(mystr[:]) # here it assumee that it start from 0th se/to length of mystr
print(mystr[::]) # here it assumee that it start from 0th se/to length of mystr
print(mystr[::5]) # here it stars from 0th to length of mystr but only print from 0 having 4 gap between them
print(mystr[2::]) # here it print from 2nd place to length of mystr
print(mystr[2::5]) ''' # here it starts from 2nd to length of mystr but print from 2nd havind 4 gap b/w them to the length of mystr
print(mystr[-5:]) # here -1 starts from back i.e. -5 means n .It print from -5 to +19
print(mystr[-3:-1]) # it print from -3 from back i.e.-3=E to (length of mystr -1)=C
print(mystr[26:28]) # here it print from(length of mystr-3) to (lemgth of mystr-1)
print(mystr[::-1]) # It simply reverse the string and then print it i.e. ECE ni hcet-b gniod si maytas
print(mystr[15:1:-2]) # It simply reverse the string from 1st to 14th place and then print it with 2 gap b\w them.
print(mystr[::559])                     # it starts default at 0 and try to find 558th character after 0th to print it but no result.

# functions of string
print(mystr.isalnum()) # it siply check whether the string stored inside mystr is alpha-numeric or not .if not output comes out false otherwise true.
print(mystr1.isalnum()) # it siply check whether the string stored inside mystr is alpha-numeric or not .if not output comes out false otherwise true.
# here alphe means no space between them & numeric means made up of numbers
print(mystr.endswith("ECE")) # It simply checking whether string is ending with ECE ,if yes o/p=true &if no o/p=false
print(mystr.count("E")) # It simply count nunber of E in string.
print(mystr.capitalize()) # It simply make first letter if string captial.
print(mystr.find("is")) # It simply find from which letter is start.
print(mystr.lower()) # It simply convert all letters of string into lower case.
print(mystr.upper()) # It sipmly convert all letter of string in upper case.
print(mystr.replace("is","are")) # It simply replace is to are.

Comments