5.1. String Str
str
is a sequence of characters
5.1.1. Syntax
>>> data = ''
>>> data = 'hello'
5.1.2. Quotes or Apostrophes
Both quotes (
"
) and apostrophes ('
) works the sameChoose one style and keep consistency in code
Python console prefers single quote (
'
) characterIt matters for
doctest
, which compares two outputs character by character
Both "
and '
works the same
>>> a = 'It is Monty Python'
>>> b = "It is Monty Python"
>>>
>>> a == b
True
Python console prefers single quote ('
) character:
>>> data = 'It is Monty Python'
>>> data
'It is Monty Python'
>>>
>>> data = "It is Monty Python"
>>> data
'It is Monty Python'
Why we have both? It's better to use double quotes, when text has apostrophes. This is also a default the behavior of Python console, which prefers less escape characters:
>>> data = 'It\'s Monty Python'
>>> data
"It's Monty Python"
>>>
>>> data = "It's Monty Python"
>>> data
"It's Monty Python"
However HTML and XML uses double quotes to enclose attribute values, hence it's better to use single quotes for the string:
>>> data = '<a href="https://python3.info">Python Book</a>'
>>> data
'<a href="https://python3.info">Python Book</a>'
>>>
>>> data = "<a href=\"https://python3.info\">Python Book</a>"
>>> data
'<a href="https://python3.info">Python Book</a>'
Errors:
>>> data = 'It's Monty Python'
Traceback (most recent call last):
SyntaxError: unterminated string literal (detected at line 1)
>>> data = "<a href="https://python3.info">Python Book</a>"
Traceback (most recent call last):
SyntaxError: invalid syntax
5.1.3. Newline and Tab
\n
- New line (ENTER)\t
- Horizontal Tab (TAB)
>>> data = 'Hello\nWorld'
>>> print(data)
Hello
World
>>> data = 'Hello\tWorld'
>>> print(data)
Hello World
5.1.4. Multiline Strings
"""
- three quotes'''
- three apostrophes
If assigned to variable, it serves as multiline str
otherwise
it's a docstring:
>>> data = """Fist line
... Second line
... Third line"""
>>> data = '''Fist line
... Second line
... Third line'''
5.1.5. Longer Strings
brackets
()
or backslashes\
can be used to split long stringsafter
\
there can't be any other character, not even a space (!)
>>> data = (
... 'First part '
... 'Second part '
... 'Third part'
... )
>>> data = 'First part ' \
... 'Second part ' \
... 'Third part'
5.1.6. Escape Characters
\'
- Single quote'
(escape in single quoted strings)\"
- Double quote"
(escape in double quoted strings)\\
- Backslash\
(to indicate, that this is not escape char)More information in Builtin Printing
>>> data = 'Hello\\World'
>>> print(data)
Hello\World
>>> data = 'Hello \'World\''
>>> print(data)
Hello 'World'
>>> data = "Hello \"World\""
>>> print(data)
Hello "World"
5.1.7. Conversion
Builtin function
str()
converts argument tostr
>>> str(1)
'1'
>>> str(1.0)
'1.0'
>>> str(True)
'True'
>>> str(None)
'None'
5.1.8. Unicode
More information in Intermediate -> Encoding -> Unicode
>>> text = 'Hello \U0001F600'
>>> print(text)
Hello 😀
>>> text = 'Hello 😀'
>>> print(text)
Hello 😀
Advanced:
>>> a = '\U0001F9D1' # 🧑
>>> b = '\U0000200D' # ''
>>> c = '\U0001F680' # 🚀
>>>
>>> astronaut = a + b + c
>>> print(astronaut)
🧑🚀
5.1.9. Recap
str
is a sequence of charactersBoth quotes (
"
) and apostrophes ('
) works the sameChoose one style and keep consistency in code
Python console prefers single quote (
'
) characterBuiltin function
str()
converts argument tostr
brackets
()
or backslashes\
can be used to split long stringsafter
\
there can't be any other character, not even a space (!)\n
- New line (ENTER)\t
- Horizontal Tab (TAB)\'
- Single quote'
(escape in single quoted strings)\"
- Double quote"
(escape in double quoted strings)\\
- Backslash\
(to indicate, that this is not escape char)
Definition:
>>> text = 'It is Monty Python'
>>> text = "It is Monty Python"
Quotes or apostrophes:
>>> text = "It's Monty Python"
>>> text = 'It\'s Monty Python'
>>>
>>> text = '<a href="https://python3.info">Python Book</a>'
>>> text = "<a href=\"https://python3.info\">Python Book</a>"
Newline and Tab:
>>> text = 'Hello\nWorld'
>>> text = 'Hello\tWorld'
Multiline:
>>> text = """Fist line
... Second line
... Third line"""
>>> text = '''Fist line
... Second line
... Third line'''
Longer:
>>> text = (
... 'First part '
... 'Second part '
... 'Third part'
... )
>>> text = 'First part ' \
... 'Second part ' \
... 'Third part'
Type conversion:
>>> str(1)
'1'
Unicode:
>>> text = 'Hello \U0001F600'
>>> print(text)
Hello 😀
5.1.10. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Type Str Define
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result_a` with value `hello`, use single apostrophes
# 2. Define `result_b` with value `hello`, use single quotes
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result_a` z wartością `hello`, użyj pojedynczych apostrofów
# 2. Zdefiniuj `result_b` z wartością `hello`, użyj pojedynczych cudzysłowów
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is str, \
'Variable `result_a` has invalid type, should be str'
>>> result_a
'hello'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is str, \
'Variable `result_b` has invalid type, should be str'
>>> result_b
'hello'
"""
# Define `result_a` with value `hello`, use single apostrophes
# type: str
result_a = ...
# Define `result_b` with value `hello`, use single quotes
result_b = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Type Str Define
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result_a` with value `hello`, use triple apostrophes
# 2. Define `result_b` with value `hello`, use triple quotes
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result_a` z wartością `hello`, użyj potrójnych apostrofów
# 2. Zdefiniuj `result_b` z wartością `hello`, użyj potrójnych cudzysłowów
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is str, \
'Variable `result_a` has invalid type, should be str'
>>> result_a
'hello'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is str, \
'Variable `result_b` has invalid type, should be str'
>>> result_b
'hello'
"""
# Define `result_a` with value `hello`, use triple apostrophes
# type: str
result_a = ...
# Define `result_b` with value `hello`, use triple quotes
result_b = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Type Str Quotes
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define variable `result: str` with value:
# He said: "It's Monty Python"
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result: str` z wartością:
# He said: "It's Monty Python"
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> print(result)
He said: "It's Monty Python"
"""
# Define variable `result: str` with value:
# He said: "It's Monty Python"
# type: str
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Type Str Unicode
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result: str` with text: 'Hello 😀'
# 2. Unicode codepoint for smiley emoticon (😀) is '\U0001F600'
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z tekstem: 'Hello 😀'
# 2. Kod znaku emotikony uśmieszek (😀) to '\U0001F600'
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> '😀' in result
True
>>> result
'Hello 😀'
"""
# Define `result: str` with text: 'Hello 😀'
# Unicode codepoint for smiley emoticon (😀) is '\U0001F600'
# type: str
result = ...