When we use strings, you will notice that we often want to use some "special characters". These special characters consist of the backslash character (\
) followed by another character.
Tab character \t
: For example, if we want to create an output of multiple columns, with each columns being separated with a tab from each other, we can use the tab character, which is represented as \t
.
# Example: List the first name, last name, and email of a person, in columns
# Separate the columns using a tab character
str_4a ="First Name\tLast Name\tEmail"
str_4b ="Panagiotis\tIpeirotis\tpanos@nyu.edu"
str_4c ="Kristaps\tPorzingis\tkporzee@nba.com"
print(str_4a)
print(str_4b)
print(str_4c)
New line character \n
: This is a special character that we use to represent a new line.
# Notice that we end the strings below with the \n character,
# which is the "new line" special character
str_5 = "Hello World!\nHello World Twice!"
print(str_5)
Backslash character \\
: In general, backslash (\
) is used to introduce special characters. If we want to type the backslash character itself, we do it by typing backslash twice in a row: \\
.
print("I want to print backslash: \\")
Quotes:
str_4 = 'This is a string within single quotes that can contain "double quotes" as part of the string'
print(str_4)
str_5 = 'If we want to have \'single quotes\' in single quoted string we should escape them'
print(str_5)
str_6 = "Similarly, if we want to have \"double quotes\" in double quoted string we should escape them\n"
print(str_6)
split
and join
¶Since we talked about special characters, let’s talk now about splitting strings, using the split()
function.
longstring.split(separator)
: split the first string (longstring) at every occurrence of the second string (separator) Outputs a list (see below).connector.join(list)
: join is the "reverse" of split, and joins all the elements of the list, using the connector
string in front.print("practical data science".split(" "))
print("billgates@microsoft.com".split("@"))
Or let's take the example from above:
str_a ="First Name\tLast Name\tEmail"
str_b ="Panagiotis\tIpeirotis\tpanos@nyu.edu"
str_c ="Kristaps\tPorzingis\tkporzee@nba.com"
print(str_a.split('\t'))
print(str_b.split('\t'))
print(str_c.split('\t'))
Notice that when we split a string and the delimeter character does not appear, then we get back the string itself, but converted into a list with a single element. (We will talk about lists in a couple of weeks)
print("hello".split(" "))
Join is the "reverse" of split, and joins all
the elements of the list, using the "string" in front.
The command below joins the elements of the line
variable using the
characters ###
as the connecting element.
line = ['Panagiotis', 'Ipeirotis', 'panos@nyu.edu']
"###".join(line)
Consider the string billgates@microsoft.com
. Write code that finds the username of the email address and the domain of the email address, using the split()
command.
email = "billgates@microsoft.com"
result = email.split("@")
print(result)
# We can also access the individual elements of a list using indexing
print("Username:", result[0])
print("Domain:", result[1])
email = "billgates@microsoft.com"
result = email.split("@")
print(result)
# And if we want to access the individual elements of a list we can use the indexing notation, as in the case of strings:
print("Username:", result[0])
print("Domain:", result[1])