bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Python/Foundations
Python•Foundations

Python Strings

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Python Strings?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___("Hello")
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Strings are Arrays
Assign String to a Variable
Quotes Inside Quotes

Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks.

'hello' is the same as "hello" .

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

Example

print("Hello")

print('Hello')

Quotes Inside Quotes

You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

Example

print("It's alright")

print("He is called 'Johnny'")

print('He is called "Johnny"')

Assign String to a Variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

Example

a = "Hello"
print(a)

Multiline Strings

You can assign a multiline string to a variable by using three quotes:

Example

a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do
eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)

Example

a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do
eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)

Note

in the result, the line breaks are inserted at the same position as in the code.

Strings are Arrays

Like many other popular programming languages, strings in Python are arrays of unicode characters.

However, Python does not have a character data type, a single character is simply a string with a length of 1.

Square brackets can be used to access elements of the string.

Example

a = "Hello, World!"

print(a[1])

Looping Through a String

Since strings are arrays, we can loop through the characters in a string, with a for loop.

Example

for x in "banana":
  print(x)

Learn more about For Loops in our Python For Loops chapter.

String Length

To get the length of a string, use the len() function.

len()

Check String

To check if a certain phrase or character is present in a string, we can use the keyword in .

Example

txt = "The best things in life are free!"
print("free" in txt)

Example

txt = "The best things in life are free!"
if "free" in txt:

  print("Yes, 'free' is present.")

Use it in an if statement

Learn more about If statements in our Python If...Else chapter.

Check if NOT

To check if a certain phrase or character is NOT present in a string, we can use the keyword not in .

Example

txt = "The best things in life are free!"
print("expensive" not in txt)

Example

txt = "The best things in life are free!"
if "expensive" not in txt:

  print("No, 'expensive' is NOT present.")

Previous

Python Casting

Next

Python - Slicing Strings