5. str

5.1. Defining str

name = ''
name = 'Jan Twardowski'       # 'Jan Twardowski'

5.1.1. Multiline str

text = """First line
Second line
Third line
"""
# 'First line\nSecond line\nThird line\n'
text = """
    First line
    Second line
    Third line
"""
# '\n        First line\n        Second line\n        Third line\n    '

5.2. Type casting to str

str('hello')        # 'hello'
str(1969)           # '1969'
str(13.37)          # '13.37'

5.3. Single or double quote?

  • " and ' works the same

  • Choose one and keep consistency in code

  • Python console uses '

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

5.3.1. When use double quotes?

my_str = 'It\'s Twardowski\'s Moon.'
my_str = "It's Twardowski's Moon."

5.3.2. When use single quotes?

  • HTML and XML uses double quotes

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

5.3.3. When use multiline?

  • Always use double quote characters to be consistent with the docstring convention PEP 257

my_str = """My name's "José Jiménez""""
my_str = '''My name's "José Jiménez"'''

5.4. Escape characters

5.4.1. New lines

\n
\r\n
../_images/type-machine.jpg

Figure 5.1. Why we have ‘\r\n’ on Windows?

5.4.2. Other escape characters

Table 5.1. Frequently used escape characters

Sequence

Description

\\

Backslash \

\'

Single quote '

\"

Double quote "

\n

New line (LF - Linefeed)

\r

Carriage Return (CR)

\t

Horizontal Tab (TAB)

Table 5.2. Less frequently used escape characters

Sequence

Description

\a

Bell (BEL)

\b

Backspace (BS)

\f

New page (FF - Form Feed)

\v

Vertical Tab (VT)

\uF680

Character with 16-bit hex value F680

\U0001F680

Character with 32-bit hex value 0001F680

\o755

ASCII character with octal value 755

\x1F680

ASCII character with hex value 1F680

print('\U0001F680')     # 🚀

5.5. Characters before strings

5.5.1. Format String

  • String interpolation (variable substitution)

  • Since Python 3.6

  • Used for str concatenation

name = 'José Jiménez'

print(f'My name... {name}')
# My name... José Jiménez
first_name = 'Jan'
last_name = 'Twardowski'

name = f'{first_name} {last_name}'
# Jan Twardowski

5.5.2. Unicode literals

  • In Python 3 str is Unicode

  • In Python 2 str is Bytes

  • In Python 3 u'...' is only for compatibility with Python 2

u'zażółć gęślą jaźń'

5.5.3. Bytes literals

  • Used while reading from low level devices and drivers

  • Used in sockets and HTTP connections

  • bytes is a sequence of octets (integers between 0 and 255)

  • bytes.decode() conversion to unicode str

  • str.encode() conversion to bytes

b'this is bytes literals'

5.5.4. Raw String

  • Escapes does not matters

r'[a-z0-9]\n'
path = r'C:\Users\Admin\file.txt'

print(path)
# C:\Users\Admin\file.txt
path = 'C:\Users\Admin\file.txt'

print(path)
# SyntaxError: (unicode error) 'unicodeescape'
#   codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
  • Problem: \Users

  • after \U... python expects Unicode codepoint in hex

  • s is invalid hexadecimal character

5.6. Getting text from user

  • input() returns str

  • Space at the end of prompt

name = input('Type your name: ')
# User inputs: Jan Twardowski

print(name)     # 'Jan Twardowski'
type(name)      # <class 'str'>
age = input('Type your age: ')
# User inputs: 42

print(age)      # '42'
type(age)       # <class 'str'>

5.7. Length of a str

len('Jan')      # 3
len('')         # 0
a = 'Jan'
len(a)          # 3

5.8. Assignments

5.8.1. Emoticon print

  • Filename: types_emoticon.py

  • Lines of code to write: 4 lines

  • Estimated time of completion: 10 min

  1. Wczytaj od użytkownika imię

  2. Wyświetl hello IMIE EMOTICON, gdzie:

    • IMIE to imie wprowadzone przez usera

    • EMOTICON to Unicode Codepoint “U+1F642”

The whys and wherefores
  • Definiowanie zmiennych

  • Korzystanie z print formatting

  • Wczytywanie tekstu od użytkownika

5.8.2. Variables and types

  • Filename: types_str_input.py

  • Lines of code to write: 3 lines

  • Estimated time of completion: 10 min

  1. Wczytaj od użytkownika imię

  2. Za pomocą f-string formatting wyświetl na ekranie:

    '''My name... "José Jiménez".
            I'm an """astronaut!"""'''
    
  3. Uwaga! Druga linijka zaczyna się od tabulacji

  4. Gdzie wartość w podwójnych cudzysłowach to ciąg od użytkownika (w przykładzie użytkownik wpisał José Jiménez)

  5. Zwróć uwagę na znaki apostrofów, cudzysłowów, tabulacji i nowych linii

  6. W ciągu do wyświetlenia nie używaj spacji ani enterów - użyj \n i \t

  7. Nie korzystaj z dodawania stringów (str + str)

The whys and wherefores
  • Definiowanie zmiennych

  • Korzystanie z print formatting

  • Wczytywanie tekstu od użytkownika