05 - Programando em Python - Tuplas e Strings
05 - Programando em Python - Tuplas e Strings
Python:
Python:
Tuplas e Strings
Tuplas e Strings
Tuplas
Tuplas
O formato geral
template % valor
Exemplo:
>>> (====)d====( ) 100
(====100====(
>>> (====)*====( ) 1
(====1.000000====(
Formatando tuplas
Formatando tuplas
Ex.:
>>> tep&!te = ')s te )d !nos'
>>> tup&! = ((+edro(, 10)
>>> tep&!te ) tup&!
(+edro te 10 !nos(
Caracter %
Para us-las:
*ro strin" iport #
join(seqncia)
Ex.:
>>> '@'.join(('usr','bin','pyt1on'))
(usr@bin@pyt1on(
>>> 'A'.join((1,2,3,3,-))
...
TypeError: se0uence ite 0: expected strin",
int *ound
>>> 'A'.join(((1(,(2(,(3(,(3(,(-())
(1A2A3A3A-(
Strings: m%todos
Strings: m%todos
lower
lower
e
e
upper
upper
&oBer()
upper()
Ex.:
>>> print 'Esper!n?!'.upper()
E5+EC6,D6
>>> print '+E de 7!r!nj! 7i!'.&oBer()
pE de &!r!nj! &i!
Strings: m%todo
Strings: m%todo
replace
replace
rep&!ce(velho,novo,n)
Ex.:
>>> s = '0ue p!rte e rep!rte, *ic! co ! !ior
p!rte'
>>> s.rep&!ce('p!rte','p!rce&!')
(0ue p!rce&! e rep!rce&!, *ic! co ! !ior
p!rce&!(
>>> s.rep&!ce('p!rte','p!rce&!',2)
(0ue p!rce&! e rep!rce&!, *ic! co ! !ior
p!rte(
Strings: m%todo
Strings: m%todo
split
split
sp&it(separador)
Ex.:
>>> s = 'xxx yyy /// xxx yyy ///'
>>> s.sp&it()
[(xxx(, (yyy(, (///(, (xxx(, (yyy(, (///(]
>>> s.sp&it((xxx()
[((, ( yyy /// (, ( yyy ///(]
Strings: m%todo
Strings: m%todo
strip
strip
strip(ch)
Ex.:
>>> ' xxx !*ds*! '.strip()
(xxx !*ds*!(
>>> 'xxx yyy /// xxx'.strip('xy ')
(///(
>>> ' xxx '.rstrip()
( xxx(
Strings: m%todo
Strings: m%todo
translate
translate
tr!ns&!te(trans)
Ex.:
>>> *ro strin" iport !Fetr!ns
>>> tr!ns = !Fetr!ns((0s(, (F/()
>>> s = '0ue surpres!: 0uebrei ! c!r!'
>>> s.tr!ns&!te(tr!ns)
(Fue /urpre/!: Fuebrei ! c!r!(