Title: String Operations
Slug: string_operations
Summary: String Operations
Date: 2016-05-01 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Python 3 has three string types

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

Create some simulated text.


In [1]:
string = 'The quick brown fox jumped over the lazy brown bear.'

Capitalize the first letter.


In [2]:
string_capitalized = string.capitalize()
string_capitalized


Out[2]:
'The quick brown fox jumped over the lazy brown bear.'

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


In [3]:
string_centered = string.center(79, '.')
string_centered


Out[3]:
'..............The quick brown fox jumped over the lazy brown bear..............'

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


In [4]:
string_counted = string.count('e', 4, len(string))
string_counted


Out[4]:
4

Locate any e's between the fifth and last character


In [5]:
string_find = string.find('e', 4, len(string))
string_find


Out[5]:
24

Are all characters are alphabet?


In [6]:
string_isalpha = string.isalpha()
string_isalpha


Out[6]:
False

Are all characters digits?


In [7]:
string_isdigit = string.isdigit()
string_isdigit


Out[7]:
False

Are all characters lower case?


In [8]:
string_islower = string.islower()
string_islower


Out[8]:
False

Are all chracters alphanumeric?


In [9]:
string_isalnum = string.isalnum()
string_isalnum


Out[9]:
False

Are all characters whitespaces?


In [10]:
string_isalnum = string.isspace()
string_isalnum


Out[10]:
False

Is the string properly titlespaced?


In [11]:
string_istitle = string.istitle()
string_istitle


Out[11]:
False

Are all the characters uppercase?


In [12]:
string_isupper = string.isupper()
string_isupper


Out[12]:
False

Return the lengths of string


In [13]:
len(string)


Out[13]:
52

Convert string to lower case


In [14]:
string_lower = string.lower()
string_lower


Out[14]:
'the quick brown fox jumped over the lazy brown bear.'

Convert string to lower case


In [15]:
string_upper = string.upper()
string_upper


Out[15]:
'THE QUICK BROWN FOX JUMPED OVER THE LAZY BROWN BEAR.'

Convert string to title case


In [16]:
string_title = string.title()
string_title


Out[16]:
'The Quick Brown Fox Jumped Over The Lazy Brown Bear.'

Convert string the inverted case


In [17]:
string_swapcase = string.swapcase()
string_swapcase


Out[17]:
'tHE QUICK BROWN FOX JUMPED OVER THE LAZY BROWN BEAR.'

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


In [18]:
string_lstrip = string.lstrip()
string_lstrip


Out[18]:
'The quick brown fox jumped over the lazy brown bear.'

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


In [19]:
string_strip = string.strip()
string_strip


Out[19]:
'The quick brown fox jumped over the lazy brown bear.'

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


In [20]:
string_rstrip = string.rstrip()
string_rstrip


Out[20]:
'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


In [21]:
string_replace = string.replace('e', 'E', 4)
string_replace


Out[21]:
'ThE quick brown fox jumpEd ovEr thE lazy brown bear.'