Operations
Strings are bits of text. They can be defined as anything between quotes:astring = "Hello world!"
astring2 = 'Hello world!'
Below code will prints out 12, because "Hello world!" is 12 characters long, including punctuation and spaces.
astring = "Hello world!"
print len(astring)
Below code will prints out 4, because the location of the first occurrence of the letter "o" is 4 characters away from the first character. Notice how there are actually two o's in the phrase - this method only recognizes the first.
astring = "Hello world!"
print astring.index("o")
These make a new string with all letters converted to uppercase and lowercase, respectively.
astring = "Hello world!"
print astring.upper()
print astring.lower()
No comments:
Post a Comment