Operace s řetězci

Regular Expressions in Python

Maria Eugenia Inzaugarat

Data Scientist

Změna velikosti písmen

my_string = "tHis Is a niCe StriNg"
  • Převod na malá písmena
print(my_string.lower())
this is a nice string
  • Převod na velká písmena
print(my_string.upper())
THIS IS A NICE STRING
Regular Expressions in Python

 

my_string = "tHis Is a niCe StriNg"

 

  • Velké první písmeno
print(my_string.capitalize())
This is a nice string
Regular Expressions in Python

Rozdělování

my_string = "This string will be split"
  • Rozdělení řetězce na seznam podřetězců
my_string.split(sep=" ", maxsplit=2)
['This', 'string', 'will be split']
my_string.rsplit(sep=" ", maxsplit=2)
['This string will', 'be', 'split']
Regular Expressions in Python

 

my_string = "This string will be split\nin two"

print(my_string)
This string will be split
in two

Regular Expressions in Python
  • Rozdělení na řádkových hranicích
my_string = "This string will be split\nin two"

 

my_string.splitlines()
['This string will be split', 'in two']
Regular Expressions in Python

Spojování

  • Spojení řetězců ze seznamu nebo jiného iterovatelného objektu

 

my_list = ["this", "would", "be", "a", "string"]
print(" ".join(my_list))
this would be a string
Regular Expressions in Python

Ořezávání znaků

  • Odstraňuje znaky zleva i zprava: .strip()

 

my_string = " This string will be stripped\n"
my_string.strip()
'This string will be stripped'
Regular Expressions in Python

 

my_string = " This string will be stripped\n"
  • Odstranění znaků z pravého konce
my_string.rstrip()
' This string will be stripped'
  • Odstranění znaků z levého konce
my_string.lstrip()
'This string will be stripped\n'
Regular Expressions in Python

Pojďme cvičit!

Regular Expressions in Python

Preparing Video For Download...