^^Strings, String Type. Python.

Strings implement all of the common sequence operations. lib/stdtypes#typesseq-common

'ab' in 'xabx'  True;      'ab' in 'xbax'   False

Additional methods lib/stdtypes#string-methods

  1. s = s.replace('≡', '≡')
  2. s = str.lstrip([chars]) Return a copy of the string with leading characters removed
  3. str.split()                   \n newline  provoca split  '12\n34'.split()  -> ['12', '34']
  4. 'sep'.join(iterable)       sep e' il separatore, di solito o vuoto '' o blank ' '
  5. ' '.join(s.split())   restituisce in riga la stringa-testo freeformatted
  6. [2, 4] + [2, 4]    >>> [2, 4, 2, 4]
  7. [2, 4] * 3           >>> [2, 4, 2, 4, 2, 4]

(create) single or double quotes

msg = "what do you like?"
msg = 'what do you like?'

immutable strings, unlike C strings.

str() return a string version of object  lib/stdtypes#str

Slicing. Slice notation: two indices separated by a colon.

Numerazione caratteri nella stringa

0 1 2 3 4 index
-5 -4 -3 -2 -1 index reverse
R o b i n word = "Robin"

Strings can be indexed;

1° character of a string has index 0, like in C.

indices negative  start counting from the right.

 

word = "Robin"

word[0] 'R' indexing. First element
word[-1] 'n' indexing reverse. Last element
word[-2] 'i' the last-but-one character
word[1:4] 'obi' slicing, start point inclusive

            end point non-inclusive

word[0:3]

word[ :3]

'Rob' the first 3
word[-3: ] 'bin' the last 3
word[:-3] 'Ro' Everything except the last 3
word[-3:-1] 'bi'             end point non-inclusive
word[0:]

word[:]

'Robin'  
s[:i] + s[i:]   equals s   concatenate
word[i:i] '' stringa vuota
len(word) 5  

 

 

escape
sequence
meaning
   
\n ASCII line feed LF
\r ASCII carriage return CR
\t ASCII horizontal tab TAB
\xhh character with hexadecimal value hh

 

reference/datamodel#the-standard-type-hierarchy

ord() converts a Unicode code point from its string form

to an integer in the range 0 - 10FFFF

chr() converts an integer in the range 0 - 10FFFF

to the corresponding length 1 string object

str.encode() convert a str to bytes (array), using the given text encoding.

UTF-8 is the default endoding

chr(0x70).encode() e' lungo 1 byte

chr(0x10FFFF).encode() e' lungo 4 byte

bytes.decode() achieve the opposite