Strings¶

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(self, old, new, count=-1, /)
    Return a copy with all occurrences of substring old replaced by new.
    
      count
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.
    
    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!

f-strings¶

In [9]:
first_name = "John"
last_name = "Doe"
age = 88
print(f"My name is {first_name} {last_name}, you can call me {first_name}.")
print(f"I'm {age} years old.")
My name is John Doe, you can call me John.
I'm 88 years old.
In [10]:
print(f"Use '=' to also print the variable name like this: {age=}")
Use '=' to also print the variable name like this: age=88

str.join()¶

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

Alternative (not as Pythonic and slower):

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

cool_python_libs = pandas
cool_python_libs += ", " + numpy
cool_python_libs += ", " + requests
print(f"Some cool python libraries: {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(f"ugly: {ugly_formatted}")
print(f"stripped: {stripped}")
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

Escape characters¶

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