4.1. Str Define

4.1.1. Definition

  • str is a sequence

Empty string:

>>> data = ''
>>> data = str()

Define string:

>>> data = 'Mark Watney'
>>> data = str('Mark Watney')

Multiline string:

>>> data =  'First line\nSecond line\nThird line'
>>> data = """First line
... Second line
... Third line"""
>>> 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."""

4.1.2. Quotes or Apostrophe

  • " and ' works the same

  • Choose one and keep consistency in code

  • Python console prefers single quote (') character

  • It matters for doctest, which compares two outputs character by character

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

Python console prefers single quote ('):

>>> data = 'My name is José Jiménez'
>>> data
'My name is José Jiménez'

Python console prefers single quote ('):

>>> data = "My name is José Jiménez"
>>> data
'My name is José Jiménez'

It's better to use double quotes, when text has apostrophes. This is the behavior of Python console:

>>> data = 'My name\'s José Jiménez'
>>> data
"My name's José Jiménez"

HTML and XML uses double quotes to enclose attribute values, hence it's better to use single quotes for the string:

>>> data = '<a href="http://python.astrotech.io">Python and Machine Learning</a>'
>>> data
'<a href="http://python.astrotech.io">Python and Machine Learning</a>'

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

>>> data = """My name's \"José Jiménez\""""
>>> data = '''My name\'s "José Jiménez"'''

4.1.3. Type Casting

Builtin function str() converts argument to str

>>> str('Moon')
'Moon'
>>> str(1969)
'1969'
>>> str(1.337)
'1.337'

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

>>> print(1969)
1969

4.1.4. Assignments

Code 4.1. Solution
"""
* Assignment: Type String Emoticon
* Required: yes
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
    1. Define `name` with value `Mark Watney`
    2. Print `Hello NAME EMOTICON`, where:
        a. NAME is a name read from user
        b. EMOTICON is Unicode Codepoint "\U0001F642"
    3. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `name` z wartością `Mark Watney`
    2. Wypisz `Hello NAME EMOTICON`, gdzie:
        a. NAME to imię wczytane od użytkownika
        b. EMOTICON to Unicode Codepoint "\U0001F642"
    3. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert result is not Ellipsis, 'Assignment solution must be in `result` instead of ... (Ellipsis)'
    >>> assert type(result) is str, 'Variable `result` has invalid type, should be str'

    >>> name in result
    True
    >>> result
    'Hello Mark Watney'
"""


name = ...  # str: Mark Watney
result = ...  # str: Hello Mark Watney

Code 4.2. Solution
"""
* Assignment: Type String Quotes
* Required: no
* Complexity: easy
* Lines of code: 1 lines
* Time: 5 min

English:
    1. To print use f-string formatting
    2. Note, that second line starts with tab
    3. Value `NAME` in double quotes is a name read from user
    4. Mind the different quotes, apostrophes, tabs and newlines
    5. Do not use neither space not enter - use `\n` and `\t`
    6. Do not use string addition (`str + str`)
    7. Run doctests - all must succeed

Polish:
    1. Do wypisania użyj f-string formatting
    2. Zauważ, że druga linijka zaczyna się od tabulacji
    3. Wartość `NAME` w podwójnych cudzysłowach to ciąg od użytkownika
    4. Zwróć uwagę na znaki apostrofów, cudzysłowów, tabulacji i nowych linii
    5. Nie używaj spacji ani klawisza enter - użyj `\n` i `\t`
    6. Nie korzystaj z dodawania stringów (`str + str`)
    7. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert result is not Ellipsis, 'Assignment solution must be in `result` instead of ... (Ellipsis)'
    >>> assert type(result) is str, 'Variable `result` has invalid type, should be str'

    >>> print(result)  # doctest: +NORMALIZE_WHITESPACE
    '''My name... "José Jiménez".
        I'm an \"\"\"astronaut!\"\"\"'''
"""

name = 'José Jiménez'

result = ...  # str: with '''My name... "José Jiménez".<newline><tab>I'm an \"\"\"astronaut!\"\"\"'''