In [1]:
my_string = 'Python is my favorite programming language!'
In [2]:
my_string
Out[2]:
'Python is my favorite programming language!'
In [3]:
type(my_string)
Out[3]:
str
In [4]:
len(my_string)
Out[4]:
43

Respecting PEP8 with long strings

In [5]:
long_story = ('Lorem ipsum dolor sit amet, consectetur adipiscing elit.' 
              'Pellentesque eget tincidunt felis. Ut ac vestibulum est.' 
              'In sed ipsum sit amet sapien scelerisque bibendum. Sed ' 
              'sagittis purus eu diam fermentum pellentesque.')
long_story
Out[5]:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.Pellentesque eget tincidunt felis. Ut ac vestibulum est.In sed ipsum sit amet sapien scelerisque bibendum. Sed sagittis purus eu diam fermentum pellentesque.'

str.replace()

If you don't know how it works, you can always check the help:

In [6]:
help(str.replace)
Help on method_descriptor:

replace(...)
    S.replace(old, new[, count]) -> str
    
    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

This will not modify my_string because replace is not done in-place.

In [7]:
my_string.replace('a', '?')
print(my_string)
Python is my favorite programming language!

You have to store the return value of replace instead.

In [8]:
my_modified_string = my_string.replace('is', 'will be')
print(my_modified_string)
Python will be my favorite programming language!

str.format()

In [9]:
secret = '{} is cool'.format('Python')
print(secret)
Python is cool
In [10]:
print('My name is {} {}, you can call me {}.'.format('John', 'Doe', 'John'))
# is the same as:
print('My name is {first} {family}, you can call me {first}.'.format(first='John', family='Doe'))
My name is John Doe, you can call me John.
My name is John Doe, you can call me John.

str.join()

In [11]:
pandas = 'pandas'
numpy = 'numpy'
requests = 'requests'
cool_python_libs = ', '.join([pandas, numpy, requests])
In [12]:
print('Some cool python libraries: {}'.format(cool_python_libs))
Some cool python libraries: pandas, numpy, requests

Alternatives (not as Pythonic and slower):

In [13]:
cool_python_libs = pandas + ', ' + numpy + ', ' + requests
print('Some cool python libraries: {}'.format(cool_python_libs))

cool_python_libs = pandas
cool_python_libs += ', ' + numpy
cool_python_libs += ', ' + requests
print('Some cool python libraries: {}'.format(cool_python_libs))
Some cool python libraries: pandas, numpy, requests
Some cool python libraries: pandas, numpy, requests

str.upper(), str.lower(), str.title()

In [14]:
mixed_case = 'PyTHoN hackER'
In [15]:
mixed_case.upper()
Out[15]:
'PYTHON HACKER'
In [16]:
mixed_case.lower()
Out[16]:
'python hacker'
In [17]:
mixed_case.title()
Out[17]:
'Python Hacker'

str.strip()

In [18]:
ugly_formatted = ' \n \t Some story to tell '
stripped = ugly_formatted.strip()

print('ugly: {}'.format(ugly_formatted))
print('stripped: {}'.format(ugly_formatted.strip()))
ugly:  
 	 Some story to tell 
stripped: Some story to tell

str.split()

In [19]:
sentence = 'three different words'
words = sentence.split()
print(words)
['three', 'different', 'words']
In [20]:
type(words)
Out[20]:
list
In [21]:
secret_binary_data = '01001,101101,11100000'
binaries = secret_binary_data.split(',')
print(binaries)
['01001', '101101', '11100000']

Calling multiple methods in a row

In [22]:
ugly_mixed_case = '   ThIS LooKs BAd '
pretty = ugly_mixed_case.strip().lower().replace('bad', 'good')
print(pretty)
this looks good

Note that execution order is from left to right. Thus, this won't work:

In [23]:
pretty = ugly_mixed_case.replace('bad', 'good').strip().lower()
print(pretty)
this looks bad
In [24]:
two_lines = 'First line\nSecond line'
print(two_lines)
First line
Second line
In [25]:
indented = '\tThis will be indented'
print(indented)
	This will be indented