5.1. String Str

  • str is a sequence of characters

5.1.1. Syntax

Empty string:

>>> data = ''

Define string:

>>> data = 'hello'

Multiline string:

>>> data =  'First line\nSecond line\nThird line'
>>> data = """First line
... Second line
... Third line"""

Longer strings (mind space at the end):

>>> data = (
...     'First part '
...     'Second part '
...     'Third part'
... )
>>> data = 'First part ' \
...        'Second part ' \
...        'Third part'

5.1.2. Quotes or Apostrophes

  • " and ' works the same

  • Choose one style and keep consistency in code

  • Python console prefers single quote (') character

  • It 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. Docstring

  • PEP 257 -- Docstring Conventions: For multiline str always use three double quote (""") characters

  • More information in Function Doctest

If assigned to variable, it serves as multiline str otherwise it's a docstring:

>>> """We choose to go to the Moon!
... We choose to go to the Moon in this decade and do the other things,
... not because they are easy, but because they are hard;
... because that goal will serve to organize and measure the best of our
... energies and skills, because that challenge is one that we are willing
... to accept, one we are unwilling to postpone, and one we intend to win,
... and the others, too."""  
>>> data = """We choose to go to the Moon!
... We choose to go to the Moon in this decade and do the other things,
... not because they are easy, but because they are hard;
... because that goal will serve to organize and measure the best of our
... energies and skills, because that challenge is one that we are willing
... to accept, one we are unwilling to postpone, and one we intend to win,
... and the others, too."""

5.1.4. Type Conversion

  • str(object) - converts object to the str

Builtin function str() converts argument to str

>>> str(1)
'1'
>>>
>>> str(1.0)
'1.0'
>>>
>>> str('one')
'one'

5.1.5. Print

  • print(object) - prints object on the standard output

  • print() - converts object to str before printing it

Builtin function print() before printing on the screen runs str() on its arguments:

>>> print(1)
1
>>>
>>> print(1.0)
1.0
>>>
>>> print('one')
one

5.1.6. Assignments

"""
* Assignment: Type Str Define
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Define `result` with value `hello`
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result` z wartością `hello`
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'
    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> result
    'hello'
"""

# Expected result: 'hello'
# type: str
result = ...

"""
* Assignment: Type Str Quotes
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Define `result: str` with value:
       He said: "It's Monty Python"
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: str` z wartością:
       He said: "It's Monty Python"
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> 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 = ...