π Working with Strings#
from friendly import *
from friendly.jupyter import friendly_tb
friendly_traceback 0.7.61; friendly 0.7.21.
Type 'Friendly' for basic help.
A lot of times you want to relay information to a user or send new instructions to a program. One common method to perform these tasks in Python is to use strings.
Defining Strings#
Recall that to represent a string you wrap it in quotes:
Single quotes
'
allows you to embed double quotes"
Double quotes
'
allows you to embed single quotes'
Triple quotes
"""
allows you to embed single and double quotes, and the string can span multiple lines
single_quote = 'Single quote allow you to embed "double" quotes in your string.'
double_quote = "Double quote allow you to embed 'single' quotes in your string."
triple_quote = """Triple quotes allows to embed "double quotes" as well as 'single quotes' in your string.
And can also span across multiple lines."""
Strings are immutable which means you cannot replace a value in a string. You need to create a new string.
Note
Immutable object - an object whose state cannot be modified after it is created
triple_quote = """This is triple quoted string using "single" quotes."""
triple_quote[35] = "'"
TypeError: 'str' object does not support item assignment
Formatting within a string#
You can add tabs to a string using \t
string = "Drexel \t Engineering"
print(string)
Drexel Engineering
You can add a line break with \n
string = "Drexel \nEngineering"
print(string)
Drexel
Engineering
Note
Did you notice the β\tβ, β\nβ above? These are called escape characters. They start with a \ (backslash). Internally, they are not interpreted as normal strings, but rather as special characters that represent something else. For example - β\tβ represents a tab. There are many more escape characters.
Length of a String#
You can find the length of a string using the built-in len()
function:
len(triple_quote)
130
String Slicing#
Since strings are a sequence of characters, you can access it through slicing and indexing just like you would with Python lists or tuples.
Strings are indexed with respect to each character in the string and the indexing begins at 0
snack = "Chocolate Cookie."
The first index starts at 0
print(snack[0])
C
The last index is the period at 16
print(snack[16])
.
You can access the end of a string using [-1] index
print(snack[-1])
.
Extracting a Substring#
You can extract a substring using range slicing [Start index (included): Stop index (excluded)]
print(snack[10:16])
Cookie
You can also use a combination of positive and negative slicing
print(snack[10:-1])
Cookie
If you do not specify a start or end index it will use the entire array
# Stop value not provided
print(snack[0:])
# Start value not provided (Stop value excluded according to syntax)
print(snack[:-1])
# This is also allowed
print(snack[:])
Chocolate Cookie.
Chocolate Cookie
Chocolate Cookie.
Using a Stride#
It is possible to use a 3rd parameter stride to specify how many characters you would like to move forward.
mixed_string = "D0r0e0x0e0l0 0E0n0g0i0n0e0e0r0i0n0g"
print(mixed_string[::2])
Drexel Engineering
Reversing a String#
Strides can be used to reverse a string
ordered_string = "ABCDEFG"
print(ordered_string[::-1])
GFEDCBA
Concatenation of String#
There are many times where you might want to join or concatenate strings. In python this can be done using the simple addition operator.
string1 = "Drexel"
string2 = "Engineering"
alma_mater = string1 + " " + string2
print(alma_mater)
Drexel Engineering
This will only work if all the datatypes are strings
string1 = "Drexel"
string2 = "Engineering"
string3 = "Class of"
year = 2027
alma_mater = string1 + " " + string2 + " " + string3 + " " + year
print(alma_mater)
TypeError: can only concatenate str (not "int") to str
string1 = "Drexel"
string2 = "Engineering"
string3 = "Class of"
year = 2027
alma_mater = string1 + " " + string2 + " " + string3 + " " + str(year)
print(alma_mater)
Drexel Engineering Class of 2027
Repeating Strings#
The multiply operator allows you to repeat strings
single_word = "hip "
line1 = single_word * 2 + "hurray! "
print(line1 * 3)
hip hip hurray! hip hip hurray! hip hip hurray!
Built-in String Methods#
Python provides many built-in methods or helper functions to manipulate strings.
Capitalize a String#
string = "drexel engineering"
print(string.capitalize())
Drexel engineering
Checks if lower case#
string = "drexel engineering"
print(string.islower())
True
Finds Substring#
Returns the lowest index in the string where the substring is found.
print(string.find("eng"))
7
Count Occurrence#
Counts how many times a substring occurs.
string = "drexel engineering " * 4
print("initial string: " + string)
print(string.count("eng"))
initial string: drexel engineering drexel engineering drexel engineering drexel engineering
4
Replace Values#
str.replace(substring, new)
: replaces all occurrences of the substring in string with new. You can also define a third argument max, which replaces at most max occurrences of substring in the string.
string1 = "hip hip hurray! hip hip hurray! hip hip hurray!"
string2 = string1.replace("hip", "Hip")
print(string1)
print(string2)
print("\nIDs")
print(id(string1))
print(id(string2))
hip hip hurray! hip hip hurray! hip hip hurray!
Hip Hip hurray! Hip Hip hurray! Hip Hip hurray!
IDs
140702799853296
140702799852912
You can see that the IDs for the string change. This is because they are immutable.
Splitting Strings#
str.split(sep="")
: splits the string according to the delimiter (space if not provided) and returns a list of substrings.
string2.split(sep=" ")
[
β 'Hip',
β 'Hip',
β 'hurray!',
β 'Hip',
β 'Hip',
β 'hurray!',
β 'Hip',
β 'Hip',
β 'hurray!'
]
Formatting Strings#
Sometimes you might want to substitute a variable into a string.
Python Version 3.6 Formatter#
print(
"{0} scored {1} points for the Drexel Dragons!".format("Coletrane Washington", 24)
) # Accessing values by position
Coletrane Washington scored 24 points for the Drexel Dragons!
print(
"{player_name} scored {points} points for the Drexel Dragons!".format(
player_name="Coletrane Washington", points=24
)
)
Coletrane Washington scored 24 points for the Drexel Dragons!
Python Version >3.6 F-strings#
f-string is a string literal that is prefixed with f
or F
. You can define identifiers to be used in your string within curly braces { }.
We recommend using F-strings because their beautiful practicality and simplicity.
player_name = "Coletrane Washington"
points = 24
print(f"{player_name} scored {points} points for the Drexel Dragons!")
Coletrane Washington scored 24 points for the Drexel Dragons!