Stringhe

Tipi di stringa

Operazioni sulle stringhe

+, *

   1 >>> 'foo' + 'bar' + 'baz'
   2 'foobarbaz'
   3 >>> 'foo' * 3
   4 'foofoofoo'
   5 >>> 'f' + 'o'*2
   6 'foo'

==, !=, <, >

   1 >>> 'python' == 'python'
   2 True
   3 >>> 'python' != 'python'
   4 False
   5 >>> 'python' != 'java'
   6 True
   7 >>> 'python' > 'java'
   8 True
   9 >>> 'a' < 'b'
  10 True

in

   1 >>> 'a' in 'abc'
   2 True
   3 >>> 'x' in 'abc'
   4 False
   5 >>> 'hell' in 'othello'
   6 True

len, min, max

   1 >>> len('abcdefghilmnopqrstuvz')
   2 21
   3 >>> min('cbead')
   4 'a'
   5 >>> max('cbead')
   6 'e'

slices

   1 >>> 'hello world!'[0]      #carattere 0
   2 'h'
   3 >>> 'hello world!'[5]      #carattere 5
   4 ' '
   5 >>> 'hello world!'[-1]     #ultimo carattere
   6 '!'
   7 >>> 'hello world!'[6:11]   #dal 6 (compreso) all'11 (escluso)
   8 'world'
   9 >>> 'hello world!'[-6:-1]  #gli indici possono anche essere negativi
  10 'world'
  11 >>> 'hello world!'[0:4]    #dallo 0 al 4 (escluso)
  12 'hell'
  13 >>> 'hello world!'[:4]     #dall'inizio al 4
  14 'hell'
  15 >>> 'hello world!'[6:]     #dal 6 (compreso) alla fine
  16 'world!'
  17 >>> 'hello world!'[:]      #dall'inizio alla fine
  18 'hello world!'
  19 >>> 'hello world!'[::-1]   #dall'inizio alla fine con 'step' di -1
  20 '!dlrow olleh'
  21 >>> 'hello world!'[1::2]   #dall'1 alla fine con 'step' di 2
  22 'el ol!'

Metodi

capitalize, title

   1 >>> 'hello world!'.capitalize()
   2 'Hello world!'
   3 >>> 'HELLO WORLD!'.capitalize()
   4 'Hello world!'
   5 >>> 'hello world!'.title()
   6 'Hello World!'
   7 >>> 'HELLO WORLD!'.title()
   8 'Hello World!'

lower, upper, swapcase

   1 >>> 'HELLO WORLD!'.lower()
   2 'hello world!'
   3 >>> 'hello world!'.upper()
   4 'HELLO WORLD!'
   5 >>> 'HELLO world!'.swapcase()
   6 'hello WORLD!'

islower, isupper, istitle

   1 >>> 'python'.islower()        #True se tutti i caratteri sono in minuscolo
   2 True
   3 >>> 'Python'.islower()
   4 False
   5 >>> 'PYTHON'.isupper()        #True se sono tutti in maiuscolo
   6 True
   7 >>> 'Python'.isupper()
   8 False
   9 >>> 'Hello World!'.istitle()  #True se tutte le parole iniziano con maiuscola
  10 True
  11 >>> 'Hello world!'.istitle()
  12 False

isalnum, isalpha, isdigit, isspace

   1 >>> 'abcXYZ123'.isalnum()     #True se tutti i caratteri sono alfanumerici 
   2 True
   3 >>> 'abc XYZ 123'.isalnum()   #lo spazio non è alfanumerico
   4 False
   5 
   6 >>> "abc".isalpha()           #True se sono tutti lettere dell'alfabeto
   7 True
   8 >>> "a b c".isalpha()
   9 False
  10 >>> "xyz123".isalpha()
  11 False
  12 >>> 'àèìòù'.isalpha()         #le accentate non sono incluse
  13 False
  14 
  15 >>> "123".isdigit()           #True se tutti i caratteri sono numeri
  16 True
  17 >>> "1 2 3".isdigit()
  18 False
  19 >>> "xyz123".isdigit()
  20 False
  21 >>> "1.0".isdigit()           #il '.' non è un numero
  22 False
  23 
  24 
  25 >>> '\t\n\x0b\x0c\r '.isspace() #True se tutti i caratteri sono whitespace
  26 True
  27 >>> ' '.isspace()
  28 True
  29 >>> ''.isspace()
  30 False
  31 >>> 'python'.isspace()
  32 False

center, ljust, rjust

   1 >>> 'python'.center(12)
   2 '   python   '
   3 >>> 'python'.center(12, '*')
   4 '***python***'
   5 >>> 'python'.ljust(12)
   6 'python      '
   7 >>> 'python'.ljust(12, '*')
   8 'python******'
   9 >>> 'python'.rjust(12)
  10 '      python'
  11 >>> 'python'.rjust(12, '*')
  12 '******python'

count

   1 >>> 'Which witch watches which Swatch watch?'.count('i')
   2 3
   3 >>> 'Which witch watches which Swatch watch?'.count('w')             # W != w
   4 5
   5 >>> 'Which witch watches which Swatch watch?'.count('ch')
   6 6
   7 >>> 'Which witch watches which Swatch watch?'.count('watch')
   8 3
   9 >>> 'Which witch watches which Swatch watch?'.count('watch', 20)     #a partire dal carattere 20
  10 2
  11 >>> 'Which witch watches which Swatch watch?'.count('watch', 0, 20)  #dal carattere 0 al 20
  12 1

encode, decode

/!\ da sistemare

   1 >>> print u'\u30ab\u30ae'.encode('utf-8') #converte da unicode alla codifica specificata
   2 
   3 >>> 'abc'.decode('ascii')                 #converte dalla codifica specificata a unicode
   4 u'abc'

expandtabs

   1 >>> '\tpython'.expandtabs()
   2 '        python'
   3 >>> '\tpython\t'.expandtabs(2)
   4 '  python  '

find, rfind

/!\ da sistemare

   1 >>> "abccba".find("b") #Restituisce la posizione in cui è presente la prima sottostringa(-1 se non è presente)
   2 1
   3 >>> "abccba".rfind("b") #Restituisce la posizione in cui è presente l'ultima sottostringa(-1 se non è presente)
   4 4

index, rindex

   1 

join

   1 >>> '-'.join(['foo', 'bar', 'baz'])
   2 'foo-bar-baz'
   3 >>> ''.join(['foo', 'bar', 'baz'])
   4 'foobarbaz'
   5 >>> 'bar'.join(['foo', 'baz'])
   6 'foobarbaz'

replace

   1 >>> 'foobarfoo'.replace('foo', 'bar')
   2 'barbarbar'
   3 >>> 'foobarfoo'.replace('bar', '')
   4 'foofoo'
   5 >>> 'foobarfoo'.replace('foo', 'baz', 1)
   6 'bazbarfoo'

split, rsplit

/!\ da completare

   1 >>> 'foo bar baz'.split()
   2 ['foo', 'bar', 'baz']
   3 
   4 >>> 'foo  bar\nbaz\rfoo\tbar'.split()       #se il separatore non è specificato splitta su tutti i whitespace
   5 ['foo', 'bar', 'baz', 'foo', 'bar']
   6 >>> 'foo|bar|baz|foo|bar|baz'.split('|')    #splitta sul separatore inserito
   7 ['foo', 'bar', 'baz', 'foo', 'bar', 'baz']
   8 >>> 'foobarbaz'.split('bar')                #il separatore può anche essere una stringa
   9 ['foo', 'baz']
  10 >>> 'foo|bar|baz|foo|bar|baz'.split('|',3)  #limita il numero massimo di split a 3
  11 ['foo', 'bar', 'baz', 'foo|bar|baz']

splitlines

   1 

startswith, endswith

   1 >>> 'Hello World!'.startswith('Hell')
   2 True
   3 >>> 'Hello World!'.startswith('World')
   4 False
   5 >>> 'Hello World!'.startswith('World', 6)
   6 True
   7 >>> 'Hello World!'.startswith('World', 6, 11)
   8 True
   9 >>> 'Hello World!'.endswith('!')
  10 True
  11 >>> 'Hello World!'.endswith('Hello')
  12 False
  13 >>> 'Hello World!'.endswith('World', 0, 11)
  14 True

strip, lstrip, rstrip

   1 >>> '\t Python \n'.strip()                  #Rimuove i whitespaces
   2 'Python'
   3 >>> '***Python***'.strip('*')
   4 'Python'
   5 >>> 'http://www.python.org/'.strip(':/tph') #Rimuove i caratteri inseriti
   6 'www.python.org'
   7 >>> '***Python***'.rstrip('*')
   8 '***Python'
   9 >>> '***Python***'.lstrip('*')
  10 'Python***'

translate

   1 

zfill

   1 >>> '123'.zfill(6)
   2 '000123'


CategoryDocumentazione

GuidaAEsempi/Tipi/Stringhe (last edited 2008-02-20 13:51:52 by Wolf)