Lesson 1 of 0
In Progress

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.

text = “text here”
description = ‘description here’
review = “review here”
blogpost = ‘blogpost here’

You can display a string literal with the print() function:

print(text)
print(description)
print(review)
print(blogpost)

You can assign a multi-line string to a variable by using three quotes:

multi_line = ‘This is
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

# default arguments
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

text = “Hello, CodeDirect!”
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

text = “Hello, CodeDirect!”
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

text = “Hello, CodeDirect!”
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

text = “Hello, CodeDirect!”
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

text = “Hello, CodeDirect!”
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

text = “Hello, CodeDirect!”
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 = “Hello, CodeDirect!”
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

text = ”    Hello, CodeDirect!    “
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

# SyntaxError: invalid syntax.
text = “My friend said, “How are you?””
print(text)


# valid syntax
text = “My friend said, \”How are you?\””
print(text)