...

/

Other Common String Methods

Other Common String Methods

We'll cover the following...

Besides formatting, strings can do a number of other useful tricks.

Press + to interact
s = '''Finished files are the re-
sult of years of scientif-
ic study combined with the
experience of years.'''
#①
print(s.splitlines()) #②
#['Finished files are the re-',
# 'sult of years of scientif-',
# 'ic study combined with the',
# 'experience of years.']
print(s.lower()) #③
#finished files are the re-
#sult of years of scientif-
#ic study combined with the
#experience of years.
print(s.lower().count('f')) #④
#6

① You can input multiline strings in the Python interactive shell. Once you start a multiline string with triple quotation marks, just hit ENTER and the interactive shell will prompt you to continue the string. Typing the closing triple quotation marks ends the string, and the next ENTER will execute the command (in this case, assigning the string to s).

② The splitlines() method takes one multiline string and returns a list of strings, one for each line of the original. Note that the carriage returns at the end of each line are not included.

③ The lower() method converts the entire string to lowercase. (Similarly, the upper() method converts a string ...

Access this course and 1400+ top-rated courses and projects.