Řetězce (Strings)

Všechny standarní sekvenční operace (indexing, slicing, multiplication, membership, length, minimum, maximum) jsou použitelné také u řetězců.

Pamatujte ale, že řetězce jsou immutable, proto následující kód nebude fungovat.

>>> website = 'http://www.python.org'
>>> website[-3:] = 'com'
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in ?
  website[-3:] = 'com'
TypeError: object doesn't support slice assignment

Formátování řetězců

Jednou ze metod jak vytisknout a formátovat řetězce operátor %, tak jako v jazyce C.

Jako hodnoty, které chceme vytisknout můžeme použít string, integer, tuple nebo dictionary.

>>> format = "Hello, %s. %s are you?"
>>> values = ('world', 'How')
>>> format % values
'Hello, world. How are you?'

%s je tzv. conversion specifiers. Označují místa, kde se má vložit hodnoty z pravé strany přiřazení.

Další možné použití:

%s - string
%i - integer
%f - floating point

Nejnovější a doporučovaný způsob formátování řetězců je pomocí metody format(). Každé položka řetězce, kterou chceme formátovat je reprezentována složenými závorkami {} a může obsahovat jméno a také informace o tom, jak správně řetězec zformátovat.

>>> "{}, {} and {}".format("first", "second", "third")
'first, second and third'
>>> "{0}, {1} and {2}".format("first", "second", "third")
'first, second and third'

Můžeme formátovat i takto:

>>> "{3} {0} {2} {1} {3} {0}".format("be", "not", "or", "to")
'to be or not to be'

Hodnoty můžeme také pojmenovávat:

>>> from math import pi
>>> "{name} is approximately {value:.2f}.".format(value=pi, name="π")
'π is approximately 3.14.'

.2f znamená, že číslo bude vytištěno s přesností na dvě desetinná místa.

Od Pythonu 3.6 můžeme formátovat řetězec i takto:

>>> from math import e
>>> f"Euler's constant is roughly {e}."
"Euler's constant is roughly 2.718281828459045."

Další možnost je například takováto:

>>> name = 'Fred'
>>> age = 42
>>> f'He said his name is {name} and he is {age} years old.'
He said his name is Fred and he is 42 years old.

Můžeme dokonce použít ve složených závorkách i Python výrazy a metody:

>>> name = 'Fred'
>>> seven = 7
>>> f'''He said his name is {name.upper()}
...    and he is {6 * seven} years old.'''
'He said his name is FRED\n    and he is 42 years old.'

Starší ekvivalent stejného kódu je:

>>> "Euler's constant is roughly {e}.".format(e=e)
"Euler's constant is roughly 2.718281828459045."

Formátování řetězců (funkce format) používá templatovací jazyk. Každá hodnota, která má být nahrazena je uložená ve složených uvozovkách {}, tzv replacement fields. Poud chceme vypsat ve výpisu složené uvozovky, musíme to udělat takto:

>>> "{{ double braces }}".format()
'{ double braces }'

Replacement Fields

Skládají se z:

Field name - index nebo indentifikátor

Conversion flag - Vykřičník následovaný jedním znakem

  • r - repr
  • s - string
  • a - ascii

Format specifier - dojtečka následovaná výrazem templatovacího jazyka

Příklady použití:

>>> "{foo} {} {bar} {}".format(1, 2, bar=4, foo=3)
'3 1 4 2'

Můžeme přistupovat také jen k části hodnoty (pole), kterou chceme vytisknout:

>>> fullname = ["Alfred", "Hitchcock"]
>>> "Mr {name[1]}".format(name=fullname)
'Mr Hitchcock'

Základní konverze

>>> print("{pi!s} {pi!r} {pi!a}".format(pi="π"))
π 'π' '\u03c0'

Floating point format specifier

>>> "The number is {num}".format(num=42)
'The number is 42'
>>> "The number is {num:f}".format(num=42)
'The number is 42.000000'

Binary format specifier

>>> "The number is {num:b}".format(num=42)
'The number is 101010'

Format specifier

  • b - binary
  • c - integer
  • d - integer vytiskne jako decimal
  • f - decimal s fixním počtem desetinných míst
  • o - integer jako osmičkové číslo

Více v dokumentaci.

Šířka zarovnání

>>> "{num:10}".format(num=3)
'         3'
>>> "{name:10}".format(name="Bob")
'Bob       '

Precison

>>> "Pi is {pi:.2f}".format(pi=pi)
'Pi is 3.14'

Formátování se zarovnáním:

>>> "{pi:10.2f}".format(pi=pi)
'      3.14'

Přesnost zarovnání se dá použít také takto:

>>> "{:.5}".format("Guido van Rossum")
'Guido'

Můžeme specifikovat několik typů zarovnání.

Zero-padded:

>>> '{:010.2f}'.format(pi)
'0000003.14'

Levé, pravé a vycentrované:

>>> print('{0:<10.2f}\n{0:^10.2f}\n{0:>10.2f}'.format(pi))
3.14
   3.14
      3.14

Můžeme specifikovat jakým znakem vyplníme volné místa, jako náhradu za mezeru:

>>> "{:$^15}".format(" I WON ")
'$$$$ I WON $$$$'

Jak formátovat čísla se znaménky?

>>> print('{0:-.2}\n{1:-.2}'.format(pi, -pi)) # Default
3.1
-3.1
>>> print('{0:+.2}\n{1:+.2}'.format(pi, -pi))
+3.1
-3.1
>>> print('{0: .2}\n{1: .2}'.format(pi, -pi))
 3.1
-3.1

Praktický příklad:

# Print a formatted price list with a given width

width = int(input('Please enter width: '))

price_width = 10
item_width  = width - price_width

header_fmt = '{{:{}}}{{:>{}}}'.format(item_width, price_width)
fmt        = '{{:{}}}{{:>{}.2f}}'.format(item_width, price_width)

print('=' * width)

print(header_fmt.format('Item', 'Price'))

print('-' * width)

print(fmt.format('Apples', 0.4))
print(fmt.format('Pears', 0.5))
print(fmt.format('Cantaloupes', 1.92))
print(fmt.format('Dried Apricots (16 oz.)', 8))
print(fmt.format('Prunes (4 lbs.)', 12))                                                                              

print('=' * width)

Výstup:

Please enter  width: 35
===================================
Item                          Price
-----------------------------------
Apples                         0.40
Pears                          0.50
Cantaloupes                    1.92
Dried  Apricots (16 oz.)       8.00
Prunes (4 lbs.)               12.00
===================================

Metody řetězců

center()

>>> "The Middle by Jimmy Eat World".center(39)
'     The Middle by Jimmy Eat World     '
>>> "The Middle by Jimmy Eat World".center(39, "*")
'*****The Middle by Jimmy Eat World*****'

find()

Vrací levý index, na kterém našel výskyt řetězce.

>>> 'With a moo-moo here, and a moo-moo there'.find('moo')
7
>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
0
>>> title.find('Python')
6
>>> title.find('Flying')
15
>>> title.find('Zirquss')
-1

join()

>>> seq = [1, 2, 3, 4, 5]
>>> sep = '+'
>>> sep.join(seq) # Trying to join a list of numbers
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found
>>> seq = ['1', '2', '3', '4', '5']
>>> sep.join(seq) # Joining a list of strings
'1+2+3+4+5'
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print('C:' + '\\'.join(dirs))
C:\usr\bin\env

Inverzní funkce je split().

lower()

Vrací lowercase verzi řetězce:

>>> 'Dance Floor'.lower()
'dance floor'

Reverzní funkce upper().

replace()

Všechny výskyty hledaného řetězce jsou nahrazeny.

>>> 'This is a test'.replace('is', 'eez')
'Theez eez a test'

split()

>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> '/usr/bin/env'.split('/')
['', 'usr', 'bin', 'env']
>>> 'Using   the   default'.split()
['Using', 'the', 'default']

strip()

Vrací řetězec bez prázdných znaků na začátku a na konci.

>>> '    internal whitespace is kept    '.strip()
'internal whitespace is kept'
{
  "data": {
    "sessionMaterial": {
      "id": "session-material:2019/tieto-ostrava-jaro:strings:0",
      "title": "Řetězce (Strings)",
      "html": "\n          \n    \n\n    <h2>&#x158;et&#x11B;zce (Strings)</h2>\n<p>V&#x161;echny standarn&#xED; sekven&#x10D;n&#xED; operace (indexing, slicing, multiplication, membership, length, minimum, maximum) jsou pou&#x17E;iteln&#xE9; tak&#xE9; u &#x159;et&#x11B;zc&#x16F;.</p>\n<p>Pamatujte ale, &#x17E;e &#x159;et&#x11B;zce jsou immutable, proto n&#xE1;sleduj&#xED;c&#xED; k&#xF3;d nebude fungovat.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; website = &apos;http://www.python.org&apos;\n&gt;&gt;&gt; website[-3:] = &apos;com&apos;\nTraceback (most recent call last):\n  File &quot;&lt;pyshell#19&gt;&quot;, line 1, in ?\n  website[-3:] = &apos;com&apos;\nTypeError: object doesn&apos;t support slice assignment</code></pre></div><h2>Form&#xE1;tov&#xE1;n&#xED; &#x159;et&#x11B;zc&#x16F;</h2>\n<p>Jednou ze metod jak vytisknout a form&#xE1;tovat &#x159;et&#x11B;zce oper&#xE1;tor %, tak jako v jazyce C.</p>\n<p>Jako hodnoty, kter&#xE9; chceme vytisknout m&#x16F;&#x17E;eme pou&#x17E;&#xED;t string, integer, tuple nebo dictionary.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; format = &quot;Hello, %s. %s are you?&quot;\n&gt;&gt;&gt; values = (&apos;world&apos;, &apos;How&apos;)\n&gt;&gt;&gt; format % values\n&apos;Hello, world. How are you?&apos;</code></pre></div><p>%s je tzv. <em>conversion specifiers</em>. Ozna&#x10D;uj&#xED; m&#xED;sta, kde se m&#xE1; vlo&#x17E;it hodnoty z prav&#xE9; strany p&#x159;i&#x159;azen&#xED;.</p>\n<p>Dal&#x161;&#xED; mo&#x17E;n&#xE9; pou&#x17E;it&#xED;:</p>\n<div class=\"highlight\"><pre><code>%s - string\n%i - integer\n%f - floating point</code></pre></div><p>Nejnov&#x11B;j&#x161;&#xED; a doporu&#x10D;ovan&#xFD; zp&#x16F;sob form&#xE1;tov&#xE1;n&#xED; &#x159;et&#x11B;zc&#x16F; je pomoc&#xED; metody <em>format()</em>. Ka&#x17E;d&#xE9; polo&#x17E;ka &#x159;et&#x11B;zce, kterou chceme form&#xE1;tovat je reprezentov&#xE1;na slo&#x17E;en&#xFD;mi z&#xE1;vorkami <em>{}</em> a m&#x16F;&#x17E;e obsahovat jm&#xE9;no a tak&#xE9; informace o tom, jak spr&#xE1;vn&#x11B; &#x159;et&#x11B;zec zform&#xE1;tovat.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{}, {} and {}&quot;.format(&quot;first&quot;, &quot;second&quot;, &quot;third&quot;)\n&apos;first, second and third&apos;\n&gt;&gt;&gt; &quot;{0}, {1} and {2}&quot;.format(&quot;first&quot;, &quot;second&quot;, &quot;third&quot;)\n&apos;first, second and third&apos;</code></pre></div><p>M&#x16F;&#x17E;eme form&#xE1;tovat i takto:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{3} {0} {2} {1} {3} {0}&quot;.format(&quot;be&quot;, &quot;not&quot;, &quot;or&quot;, &quot;to&quot;)\n&apos;to be or not to be&apos;</code></pre></div><p>Hodnoty m&#x16F;&#x17E;eme tak&#xE9; pojmenov&#xE1;vat:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; from math import pi\n&gt;&gt;&gt; &quot;{name} is approximately {value:.2f}.&quot;.format(value=pi, name=&quot;&#x3C0;&quot;)\n&apos;&#x3C0; is approximately 3.14.&apos;</code></pre></div><p><em>.2f</em> znamen&#xE1;, &#x17E;e &#x10D;&#xED;slo bude vyti&#x161;t&#x11B;no s p&#x159;esnost&#xED; na dv&#x11B; desetinn&#xE1; m&#xED;sta.</p>\n<p>Od Pythonu 3.6 m&#x16F;&#x17E;eme form&#xE1;tovat &#x159;et&#x11B;zec i takto:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; from math import e\n&gt;&gt;&gt; f&quot;Euler&apos;s constant is roughly {e}.&quot;\n&quot;Euler&apos;s constant is roughly 2.718281828459045.&quot;</code></pre></div><p>Dal&#x161;&#xED; mo&#x17E;nost je nap&#x159;&#xED;klad takov&#xE1;to:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; name = &apos;Fred&apos;\n&gt;&gt;&gt; age = 42\n&gt;&gt;&gt; f&apos;He said his name is {name} and he is {age} years old.&apos;\nHe said his name is Fred and he is 42 years old.</code></pre></div><p>M&#x16F;&#x17E;eme dokonce pou&#x17E;&#xED;t ve slo&#x17E;en&#xFD;ch z&#xE1;vork&#xE1;ch i Python v&#xFD;razy a metody:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; name = &apos;Fred&apos;\n&gt;&gt;&gt; seven = 7\n&gt;&gt;&gt; f&apos;&apos;&apos;He said his name is {name.upper()}\n...    and he is {6 * seven} years old.&apos;&apos;&apos;\n&apos;He said his name is FRED\\n    and he is 42 years old.&apos;</code></pre></div><p>Star&#x161;&#xED; ekvivalent stejn&#xE9;ho k&#xF3;du je:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;Euler&apos;s constant is roughly {e}.&quot;.format(e=e)\n&quot;Euler&apos;s constant is roughly 2.718281828459045.&quot;</code></pre></div><p>Form&#xE1;tov&#xE1;n&#xED; &#x159;et&#x11B;zc&#x16F; (funkce format) pou&#x17E;&#xED;v&#xE1; templatovac&#xED; jazyk. Ka&#x17E;d&#xE1; hodnota, kter&#xE1; m&#xE1; b&#xFD;t nahrazena je ulo&#x17E;en&#xE1; ve slo&#x17E;en&#xFD;ch uvozovk&#xE1;ch <em>{}</em>, tzv <em>replacement fields</em>. Poud chceme vypsat ve v&#xFD;pisu slo&#x17E;en&#xE9; uvozovky, mus&#xED;me to ud&#x11B;lat takto:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{{ double braces }}&quot;.format()\n&apos;{ double braces }&apos;</code></pre></div><h3>Replacement Fields</h3>\n<p>Skl&#xE1;daj&#xED; se z:</p>\n<p><strong>Field name</strong> - index nebo indentifik&#xE1;tor</p>\n<p><strong>Conversion flag</strong> - Vyk&#x159;i&#x10D;n&#xED;k n&#xE1;sledovan&#xFD; jedn&#xED;m znakem</p>\n<ul>\n<li>r - repr</li>\n<li>s - string</li>\n<li>a - ascii</li>\n</ul>\n<p><strong>Format specifier</strong> - dojte&#x10D;ka n&#xE1;sledovan&#xE1; v&#xFD;razem templatovac&#xED;ho jazyka</p>\n<p>P&#x159;&#xED;klady pou&#x17E;it&#xED;:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{foo} {} {bar} {}&quot;.format(1, 2, bar=4, foo=3)\n&apos;3 1 4 2&apos;</code></pre></div><p>M&#x16F;&#x17E;eme p&#x159;istupovat tak&#xE9; jen k &#x10D;&#xE1;sti hodnoty (pole), kterou chceme vytisknout:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; fullname = [&quot;Alfred&quot;, &quot;Hitchcock&quot;]\n&gt;&gt;&gt; &quot;Mr {name[1]}&quot;.format(name=fullname)\n&apos;Mr Hitchcock&apos;</code></pre></div><h3>Z&#xE1;kladn&#xED; konverze</h3>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; print(&quot;{pi!s} {pi!r} {pi!a}&quot;.format(pi=&quot;&#x3C0;&quot;))\n&#x3C0; &apos;&#x3C0;&apos; &apos;\\u03c0&apos;</code></pre></div><p>Floating point <em>format specifier</em></p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;The number is {num}&quot;.format(num=42)\n&apos;The number is 42&apos;\n&gt;&gt;&gt; &quot;The number is {num:f}&quot;.format(num=42)\n&apos;The number is 42.000000&apos;</code></pre></div><p>Binary <em>format specifier</em></p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;The number is {num:b}&quot;.format(num=42)\n&apos;The number is 101010&apos;</code></pre></div><p><strong>Format specifier</strong></p>\n<ul>\n<li>b - binary</li>\n<li>c - integer</li>\n<li>d - integer vytiskne jako decimal</li>\n<li>f - decimal s fixn&#xED;m po&#x10D;tem desetinn&#xFD;ch m&#xED;st</li>\n<li>o - integer jako osmi&#x10D;kov&#xE9; &#x10D;&#xED;slo</li>\n</ul>\n<p>V&#xED;ce v dokumentaci.</p>\n<h3>&#x160;&#xED;&#x159;ka zarovn&#xE1;n&#xED;</h3>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{num:10}&quot;.format(num=3)\n&apos;         3&apos;\n&gt;&gt;&gt; &quot;{name:10}&quot;.format(name=&quot;Bob&quot;)\n&apos;Bob       &apos;</code></pre></div><h3>Precison</h3>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;Pi is {pi:.2f}&quot;.format(pi=pi)\n&apos;Pi is 3.14&apos;</code></pre></div><p>Form&#xE1;tov&#xE1;n&#xED; se zarovn&#xE1;n&#xED;m:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{pi:10.2f}&quot;.format(pi=pi)\n&apos;      3.14&apos;</code></pre></div><p>P&#x159;esnost zarovn&#xE1;n&#xED; se d&#xE1; pou&#x17E;&#xED;t tak&#xE9; takto:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{:.5}&quot;.format(&quot;Guido van Rossum&quot;)\n&apos;Guido&apos;</code></pre></div><p>M&#x16F;&#x17E;eme specifikovat n&#x11B;kolik typ&#x16F; zarovn&#xE1;n&#xED;.</p>\n<p>Zero-padded:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &apos;{:010.2f}&apos;.format(pi)\n&apos;0000003.14&apos;</code></pre></div><p>Lev&#xE9;, prav&#xE9; a vycentrovan&#xE9;:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; print(&apos;{0:&lt;10.2f}\\n{0:^10.2f}\\n{0:&gt;10.2f}&apos;.format(pi))\n3.14\n   3.14\n      3.14</code></pre></div><p>M&#x16F;&#x17E;eme specifikovat jak&#xFD;m znakem vypln&#xED;me voln&#xE9; m&#xED;sta, jako n&#xE1;hradu za mezeru:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;{:$^15}&quot;.format(&quot; I WON &quot;)\n&apos;$$$$ I WON $$$$&apos;</code></pre></div><p>Jak form&#xE1;tovat &#x10D;&#xED;sla se znam&#xE9;nky?</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; print(&apos;{0:-.2}\\n{1:-.2}&apos;.format(pi, -pi)) # Default\n3.1\n-3.1\n&gt;&gt;&gt; print(&apos;{0:+.2}\\n{1:+.2}&apos;.format(pi, -pi))\n+3.1\n-3.1\n&gt;&gt;&gt; print(&apos;{0: .2}\\n{1: .2}&apos;.format(pi, -pi))\n 3.1\n-3.1</code></pre></div><p>Praktick&#xFD; p&#x159;&#xED;klad:</p>\n<div class=\"highlight\"><pre><code># Print a formatted price list with a given width\n\nwidth = int(input(&apos;Please enter width: &apos;))\n\nprice_width = 10\nitem_width  = width - price_width\n\nheader_fmt = &apos;{{:{}}}{{:&gt;{}}}&apos;.format(item_width, price_width)\nfmt        = &apos;{{:{}}}{{:&gt;{}.2f}}&apos;.format(item_width, price_width)\n\nprint(&apos;=&apos; * width)\n\nprint(header_fmt.format(&apos;Item&apos;, &apos;Price&apos;))\n\nprint(&apos;-&apos; * width)\n\nprint(fmt.format(&apos;Apples&apos;, 0.4))\nprint(fmt.format(&apos;Pears&apos;, 0.5))\nprint(fmt.format(&apos;Cantaloupes&apos;, 1.92))\nprint(fmt.format(&apos;Dried Apricots (16 oz.)&apos;, 8))\nprint(fmt.format(&apos;Prunes (4 lbs.)&apos;, 12))                                                                              \n\nprint(&apos;=&apos; * width)</code></pre></div><p>V&#xFD;stup:</p>\n<div class=\"highlight\"><pre><code>Please enter  width: 35\n===================================\nItem                          Price\n-----------------------------------\nApples                         0.40\nPears                          0.50\nCantaloupes                    1.92\nDried  Apricots (16 oz.)       8.00\nPrunes (4 lbs.)               12.00\n===================================</code></pre></div><h2>Metody &#x159;et&#x11B;zc&#x16F;</h2>\n<h3>center()</h3>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &quot;The Middle by Jimmy Eat World&quot;.center(39)\n&apos;     The Middle by Jimmy Eat World     &apos;\n&gt;&gt;&gt; &quot;The Middle by Jimmy Eat World&quot;.center(39, &quot;*&quot;)\n&apos;*****The Middle by Jimmy Eat World*****&apos;</code></pre></div><h3>find()</h3>\n<p>Vrac&#xED; lev&#xFD; index, na kter&#xE9;m na&#x161;el v&#xFD;skyt &#x159;et&#x11B;zce.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &apos;With a moo-moo here, and a moo-moo there&apos;.find(&apos;moo&apos;)\n7\n&gt;&gt;&gt; title = &quot;Monty Python&apos;s Flying Circus&quot;\n&gt;&gt;&gt; title.find(&apos;Monty&apos;)\n0\n&gt;&gt;&gt; title.find(&apos;Python&apos;)\n6\n&gt;&gt;&gt; title.find(&apos;Flying&apos;)\n15\n&gt;&gt;&gt; title.find(&apos;Zirquss&apos;)\n-1</code></pre></div><h3>join()</h3>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; seq = [1, 2, 3, 4, 5]\n&gt;&gt;&gt; sep = &apos;+&apos;\n&gt;&gt;&gt; sep.join(seq) # Trying to join a list of numbers\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in ?\nTypeError: sequence item 0: expected string, int found\n&gt;&gt;&gt; seq = [&apos;1&apos;, &apos;2&apos;, &apos;3&apos;, &apos;4&apos;, &apos;5&apos;]\n&gt;&gt;&gt; sep.join(seq) # Joining a list of strings\n&apos;1+2+3+4+5&apos;\n&gt;&gt;&gt; dirs = &apos;&apos;, &apos;usr&apos;, &apos;bin&apos;, &apos;env&apos;\n&gt;&gt;&gt; &apos;/&apos;.join(dirs)\n&apos;/usr/bin/env&apos;\n&gt;&gt;&gt; print(&apos;C:&apos; + &apos;\\\\&apos;.join(dirs))\nC:\\usr\\bin\\env</code></pre></div><p>Inverzn&#xED; funkce je <em>split()</em>.</p>\n<h3>lower()</h3>\n<p>Vrac&#xED; lowercase verzi &#x159;et&#x11B;zce:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &apos;Dance Floor&apos;.lower()\n&apos;dance floor&apos;</code></pre></div><p>Reverzn&#xED; funkce <em>upper()</em>.</p>\n<h3>replace()</h3>\n<p>V&#x161;echny v&#xFD;skyty hledan&#xE9;ho &#x159;et&#x11B;zce jsou nahrazeny.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &apos;This is a test&apos;.replace(&apos;is&apos;, &apos;eez&apos;)\n&apos;Theez eez a test&apos;</code></pre></div><h3>split()</h3>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &apos;1+2+3+4+5&apos;.split(&apos;+&apos;)\n[&apos;1&apos;, &apos;2&apos;, &apos;3&apos;, &apos;4&apos;, &apos;5&apos;]\n&gt;&gt;&gt; &apos;/usr/bin/env&apos;.split(&apos;/&apos;)\n[&apos;&apos;, &apos;usr&apos;, &apos;bin&apos;, &apos;env&apos;]\n&gt;&gt;&gt; &apos;Using   the   default&apos;.split()\n[&apos;Using&apos;, &apos;the&apos;, &apos;default&apos;]</code></pre></div><h3>strip()</h3>\n<p>Vrac&#xED; &#x159;et&#x11B;zec bez pr&#xE1;zdn&#xFD;ch znak&#x16F; na za&#x10D;&#xE1;tku a na konci.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; &apos;    internal whitespace is kept    &apos;.strip()\n&apos;internal whitespace is kept&apos;</code></pre></div>\n\n\n        "
    }
  }
}