Pylerno
English

Strings

Build text values and distinguish written escape sequences from displayed characters.

On this page

The concept

A string stores Unicode text. Single and double quotes are interchangeable delimiters when used consistently. Triple quotes can create a multiline string value; in this lesson that value is program data, not an explanation embedded in the source file. + joins strings and * repeats them.

Some characters need escaping inside a quoted string. A backslash can introduce an escape such as a newline, or \ can represent a literal backslash. Unicode lets Python represent many writing systems. However, len counts code points, which is not always the same as the number of visible symbols in a complex emoji or a combined-character sequence.

Worked example

python
first_name = "Sara"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name)
print(len("Python"))
print("ha" * 3)
print('She said: "Hello"')
print("C:\\Users\\Student")
message = "Learning\nimproves with practice."
print(message)

Trace the result

  1. first_name and last_name are joined with an explicit space; concatenation does not insert one automatically.
  2. The repetition example produces three copies of the same short string.
  3. The path and multiline string show why source spelling and displayed text can differ. Read the expected output rather than counting backslashes in the source.
text
Sara Smith
6
hahaha
She said: "Hello"
C:\Users\Student
Learning
improves with practice.
Line-by-line walkthrough

Line 1: first_name = "Sara"

Evaluate 'Sara', then bind or update first_name with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 2: last_name = "Smith"

Evaluate 'Smith', then bind or update last_name with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 3: full_name = first_name + " " + last_name

Evaluate first_name + ' ' + last_name, then bind or update full_name with the result. Assignment itself does not imply a deep copy.

Line 4: print(full_name)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 5: print(len("Python"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 6: print("ha" * 3)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: print('She said: "Hello"')

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print("C:\\Users\\Student")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 9: message = "Learning\nimproves with practice."

Evaluate 'Learning\nimproves with practice.', then bind or update message with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 10: print(message)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

len is not a universal screen-width measurement. For files, choosing a text encoding is a separate responsibility addressed later.

Check your understanding

What does this program print?

python
print(len("007"))
Choose one answer

Search documentation

Share this page