Lesson #5: Strings
As you’ve already seen, you can create a string by surrounding some text with quotation marks. You can use either single quotes ” or double quotes “” to create a string as long as you use the same type at the beginning and end of the string.
description = ‘description here’
review = “review here”
blogpost = ‘blogpost here’
You can display a string literal with the print()
function:
print(description)
print(review)
print(blogpost)
You can assign a multi-line string to a variable by using three quotes:
how we assign,
a multi-line string.’”
print(multi_line)
String Format
In Python, we can combine strings and numbers by using the format()
method. The method format()
takes any number of parameters, reads the type of arguments passed to it, and formats it according to the format codes defined in the string.
Example
name = ‘Nick’
age = 34
phrase = “My name is {}, and I am {} years old”
print(phrase.format(name, age))
# positional arguments
name = ‘Nick’
age = 34
phrase = “My name is {0}, and I am {1} years old”
print(phrase.format(name, age))
Accessing characters
Square brackets can be used to access elements of the string.
Example
print(text[2]) # the output is the letter ‘l’
print(text[4]) # the output is the letter ‘o’
print(text[0]) # the output is the letter ‘H’
đź’ˇ Note: the first character has the position 0.
Length
In order to get the length of a string, you need to use the built-in function len()
.
Example
product_title = “Adidas running shoes”
print(len(text) # the output is 18
print(len(product_title) # the output is 20
Count
The function count()
counts the number of letters in a string.
Example
print(text.count(‘e’) # the output is 3
print(text.count(‘l’) # the output is 2
Slicing
You can use the slice syntax when you want to return a range of characters. Specify the start index and the end index, separated by a colon, to return a part of the string.
Example
print(text[0:5]) # the output is Hello
✏️ Reminder: The first character has index 0.
Upper case
You can use the upper()
method when you want to return a string in upper case.
Example
print(text.upper()) # the output is HELLO, CODEDIRECT!
Lower case
You can use the lower()
method when you want to return a string in lower case.
Example
print(text.lower()) # the output is hello, codedirect!
Split
You can use the split()
method to split strings into substrings by providing a string separator. The output would be a list of substrings.
Example
text_2 = “Hello CodeDirect!”
print(text.split(“,”)) # the output is [‘Hello’, ‘CodeDirect!’]
print(text_2.split(” “)) # the output is [‘Hello’, ‘CodeDirect!’]
Strip
You can use the strip()
method to remove the space before and/or after the actual text.
Example
print(text.strip()) # the output is “Hello CodeDirect!”
Escape Character
If you try to use double quotes inside a string delimited by double quotes, you’ll get an error SyntaxError: invalid syntax
. To fix this problem, use the escape character \"
:
Example
text = “My friend said, “How are you?””
print(text)
# valid syntax
text = “My friend said, \”How are you?\””
print(text)