03 More on String

Here some of the common string methods are shown here. You can get more on python.org


String Slices:

Standard zero based index numbers are used at the start of the string.  For reverse counting, index starts from -1.
str = "Python"

print (str)           # Prints complete string
print (str[0])       # Prints first character of the string
print (str[5])       # Prints fifth character of the string
print (str[-1])      # For reverse counting, character goes like, -1,-2,-3
print (str[-6])

In python shell you will see following output,


More on String:

text = "I like Python"            #text is string here

print (text)                             # print the whole string
print ("Guys,", text)              # print texts and then string

print (text.upper())                # make the string uppercase
print (text.lower())                # make the string lowercase

print (text.count("i"))           # counts number of i in string
print (text.count("I"))
print (text.count(" "))           # counts number of space in string


print (len(text))                     # counts the length of the string

In python shell you will see following output,