6.1. String Str — Python
stris a sequence of characters
Example:
>>> name = 'Alice' >>> color = 'red' >>> text = 'Hello World'
6.1.1. Syntax
Str can have any length: zero, one or many characters (including spaces, etc.)
>>> data = '' >>> data = 'a' >>> data = 'abc' >>> data = 'abc def'
6.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 = '<img src="myfile.png">' >>> data '<img src="myfile.png">' >>> >>> data = "<img src=\"myfile.png\">" >>> data '<img src="myfile.png">'
Errors:
>>> data = 'It's Monty Python' Traceback (most recent call last): SyntaxError: unterminated string literal (detected at line 1)
>>> data = "<img src="myfile.png">" Traceback (most recent call last): SyntaxError: invalid syntax. Is this intended to be part of the string?
6.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
6.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'''
6.1.5. 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"
6.1.6. Conversion
Builtin function
str()converts argument tostr
6.1.7. Unicode
\U- UTF-32 character codepoint (8 hex digits), example:\U0001F600\u- UTF-16 character codepoint (4 hex digits), example:\uF600Codepoint notation
U+1F600is the same as\U0001F600in PythonYou can use either codepoint
\U0001F600or emoji 😀 in your source codeMore 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) 🧑🚀
6.1.8. Contains
inchecks whether value is in sequenceO(n) - contains in
str
>>> 'P' in 'Python' True >>> >>> 'p' in 'Python' False
>>> 'Python' in 'Python' True >>> >>> 'Py' in 'Python' True
6.1.9. Recap
stris 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\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:
Unicode:
>>> text = 'Hello \U0001F600' >>> print(text) Hello 😀
6.1.10. Assignments
# %% About # - Name: Type Str Define # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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 # %% English # 1. Define `result1` with value `hello`, use single apostrophes # 2. Define `result2` with value `hello`, use single quotes # 3. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj `result1` z wartością `hello`, użyj pojedynczych apostrofów # 2. Zdefiniuj `result2` z wartością `hello`, użyj pojedynczych cudzysłowów # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> print(result1) # hello # # >>> print(result2) # hello # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result1' in globals(), \ 'Variable `result1` is not defined; assign result of your program to it.' >>> assert result1 is not Ellipsis, \ 'Variable `result1` has an invalid value; assign result of your program to it.' >>> assert type(result1) is str, \ 'Variable `result1` has an invalid type; expected: `str`.' >>> result1 'hello' >>> assert 'result2' in globals(), \ 'Variable `result2` is not defined; assign result of your program to it.' >>> assert result2 is not Ellipsis, \ 'Variable `result2` has an invalid value; assign result of your program to it.' >>> assert type(result2) is str, \ 'Variable `result2` has an invalid type; expected: `str`.' >>> result2 'hello' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result1: str result2: str # %% Data # %% Result result1 = ... result2 = ...
# %% About # - Name: Type Str Define # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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 # %% English # 1. Define `result1` with value `hello`, use triple apostrophes # 2. Define `result2` with value `hello`, use triple quotes # 3. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj `result1` z wartością `hello`, użyj potrójnych apostrofów # 2. Zdefiniuj `result2` z wartością `hello`, użyj potrójnych cudzysłowów # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> print(result1) # hello # # >>> print(result2) # hello # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result1' in globals(), \ 'Variable `result1` is not defined; assign result of your program to it.' >>> assert result1 is not Ellipsis, \ 'Variable `result1` has an invalid value; assign result of your program to it.' >>> assert type(result1) is str, \ 'Variable `result1` has an invalid type; expected: `str`.' >>> result1 'hello' >>> assert 'result2' in globals(), \ 'Variable `result2` is not defined; assign result of your program to it.' >>> assert result2 is not Ellipsis, \ 'Variable `result2` has an invalid value; assign result of your program to it.' >>> assert type(result2) is str, \ 'Variable `result2` has an invalid type; expected: `str`.' >>> result2 'hello' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result1: str result2: str # %% Data # %% Result result1 = ... result2 = ...
# %% About # - Name: Type Str Quotes # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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 # %% 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ść # %% Expected # >>> print(result) # He said: "It's Monty Python" # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> from pprint import pprint >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is str, \ 'Variable `result` has an invalid type; expected: `str`.' >>> print(result) He said: "It's Monty Python" """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: str # %% Data # %% Result result = ...
# %% About # - Name: Type Str Unicode # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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 # %% 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ść # %% Expected # >>> print(result) # Hello 😀 # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is str, \ 'Variable `result` has an invalid type; expected: `str`.' >>> '😀' in result True >>> result 'Hello 😀' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: str # %% Data # %% Result result = ...