Operações com strings

Expressões regulares em Python

Maria Eugenia Inzaugarat

Data Scientist

Ajustando maiúsculas/minúsculas

my_string = "tHis Is a niCe StriNg"
  • Converter para minúsculas
print(my_string.lower())
this is a nice string
  • Converter para MAIÚSCULAS
print(my_string.upper())
THIS IS A NICE STRING
Expressões regulares em Python

 

my_string = "tHis Is a niCe StriNg"

 

  • Colocar a 1ª letra em maiúscula
print(my_string.capitalize())
This is a nice string
Expressões regulares em Python

Dividindo

my_string = "This string will be split"
  • Dividir uma string em uma lista de substrings
my_string.split(sep=" ", maxsplit=2)
['This', 'string', 'will be split']
my_string.rsplit(sep=" ", maxsplit=2)
['This string will', 'be', 'split']
Expressões regulares em Python

 

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

print(my_string)
This string will be split
in two

Expressões regulares em Python
  • Quebrar nas quebras de linha
my_string = "This string will be split\nin two"

 

my_string.splitlines()
['This string will be split', 'in two']
Expressões regulares em Python

Juntando

  • Concatenar strings de uma lista ou outro iterável

 

my_list = ["this", "would", "be", "a", "string"]
print(" ".join(my_list))
this would be a string
Expressões regulares em Python

Removendo caracteres

  • Remove caracteres dos dois lados: .strip()

 

my_string = " This string will be stripped\n"
my_string.strip()
'This string will be stripped'
Expressões regulares em Python

 

my_string = " This string will be stripped\n"
  • Remove caracteres do fim (direita)
my_string.rstrip()
' This string will be stripped'
  • Remove caracteres do início (esquerda)
my_string.lstrip()
'This string will be stripped\n'
Expressões regulares em Python

Vamos praticar!

Expressões regulares em Python

Preparing Video For Download...