Basic String Operations - shahzade baujiti

Breaking

Wednesday, April 24, 2019

Basic String Operations

Basic String 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)
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 

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")
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 

These make a new string with all letters converted to uppercase and lowercase, respectively.
astring = "Hello world!"
print astring.upper()
print astring.lower()
Copy
Note: Copy and Paste Code into Compiler
Open Compiler

PreviousNext

No comments:

Post a Comment