String Operations

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.

Python 3 has three string types

  • str() is for unicode
  • bytes() is for binary data
  • bytesarray() mutable variable of bytes

Create some simulated text.

string = 'The quick brown fox jumped over the lazy brown bear.'

Capitalize the first letter.

string_capitalized = string.capitalize()
string_capitalized
'The quick brown fox jumped over the lazy brown bear.'

Center the string with periods on either side, for a total of 79 characters

string_centered = string.center(79, '.')
string_centered
'..............The quick brown fox jumped over the lazy brown bear..............'

Count the number of e's between the fifth and last character

string_counted = string.count('e', 4, len(string))
string_counted
4

Locate any e's between the fifth and last character

string_find = string.find('e', 4, len(string))
string_find
24

Are all characters are alphabet?

string_isalpha = string.isalpha()
string_isalpha
False

Are all characters digits?

string_isdigit = string.isdigit()
string_isdigit
False

Are all characters lower case?

string_islower = string.islower()
string_islower
False

Are all chracters alphanumeric?

string_isalnum = string.isalnum()
string_isalnum
False

Are all characters whitespaces?

string_isalnum = string.isspace()
string_isalnum
False

Is the string properly titlespaced?

string_istitle = string.istitle()
string_istitle
False

Are all the characters uppercase?

string_isupper = string.isupper()
string_isupper
False

Return the lengths of string

len(string)
52

Convert string to lower case

string_lower = string.lower()
string_lower
'the quick brown fox jumped over the lazy brown bear.'

Convert string to lower case

string_upper = string.upper()
string_upper
'THE QUICK BROWN FOX JUMPED OVER THE LAZY BROWN BEAR.'

Convert string to title case

string_title = string.title()
string_title
'The Quick Brown Fox Jumped Over The Lazy Brown Bear.'

Convert string the inverted case

string_swapcase = string.swapcase()
string_swapcase
'tHE QUICK BROWN FOX JUMPED OVER THE LAZY BROWN BEAR.'

Remove all leading whitespaces (i.e. to the left)

string_lstrip = string.lstrip()
string_lstrip
'The quick brown fox jumped over the lazy brown bear.'

Remove all leading and trailing whitespaces (i.e. to the left and right)

string_strip = string.strip()
string_strip
'The quick brown fox jumped over the lazy brown bear.'

Remove all trailing whitespaces (i.e. to the right)

string_rstrip = string.rstrip()
string_rstrip
'The quick brown fox jumped over the lazy brown bear.'

Replace lower case e's with upper case E's, to a maximum of 4

string_replace = string.replace('e', 'E', 4)
string_replace
'ThE quick brown fox jumpEd ovEr thE lazy brown bear.'