-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path008_strings.py
More file actions
135 lines (91 loc) · 2.38 KB
/
Copy path008_strings.py
File metadata and controls
135 lines (91 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# String de uma linha
formasString = "Hello World"
formasString2 = 'Hello World'
# String de múltiplas linhas
formasString3 = """Hello
World
"""
formasString4 = '''Hello
World
'''
print(type(formasString))
print(type(formasString2))
print(type(formasString3))
print(type(formasString4))
############## Strings são listas
print(formasString[0])
# Em python, tem-se index negativo para qualquer estrutura indexada
"""
string: Hello
index: 01234
54321 (sendo negativo)
"""
print(formasString[0], formasString[-len(formasString)])
print(formasString[len(formasString)-1], formasString[-1])
############## String são imutáveis
teste = "Teste"
# teste[0] = "A"
############## Percorrendo um string
nome = input("Digite um nome: ")
for letra in nome:
print(letra)
############## Verificando se uma string está em outra
frase = "Hello World"
if "Hello" in frase:
print("Hello esta na frase")
if "Hi" in frase:
print("Hi esta na frase")
# Podemos verificar se a string não está em outra (apenas utilizando o not)
if "Hi" not in frase:
print("Hi não esta na frase")
############## Slicing Strings - pegar uma parte da string
frase = "Hello, World!"
"""
[i:e] -> retorna uma string com os caracteres dos índices de i até e-1
[:e] -> retorna uma string com os caracteres dos índices 0 até e-1
[i:] -> retorna uma string com os caracteres dos índices i até o final
"""
print(frase[0:5])
print(frase[:10])
print(frase[1:])
# Podemos utilizar índices negativos também
print(frase[-5:-2])
############## Caracteres de escape
"""
\n -> quebra de linha
\t -> tabulação
"""
print("\\ \'Ignorando aspas\' \n\n")
print("\tOii")
############## Métodos
### Retornando a string alterada
s1 = "hElLo WORLD"
upper = s1.upper()
lower = s1.lower()
title = s1.title()
replace = s1.replace("hElLo", "Hi")
print(s1)
print(upper)
print(lower)
print(title)
print(replace)
### string -> list
s1 = "10, 20, 30"
l1 = s1.split()
l2 = s1.split(",")
print(l1, type(l1))
print(l2, type(l2))
s2 = "10.....1000"
print(s2.split("....."))
### Removendo caracteres do começo e/ou fim
# strip() -> remove do começo e fim
# rstrip() -> remove do final
# lstrip() -> remove do começo
s1 = " Teste "
s2 = "-------Teste------"
strip1 = s1.strip()
strip2 = s2.strip('-')
print(s1, len(s1))
print(strip1, len(strip1))
print(s2, len(s2))
print(strip2, len(strip2))