Python-Ref > String manipulation > String encodings > Basics and the ASCII character set
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

Basics and the ASCII character set

The ASCII encoding defines numerical codes (from 0-127, that is 7-bit number) for letters and other characters used in computers. The character set described by ASCII is large enough for English text, but does not contain any national characters, such as α, ř, etc.
Because ASCII uses only 128 numbers and data in computers are organised in bytes of 8-bits, 128 other characters might be encoded inside a byte. The assignment of such characters is specified by different national encodings, such as iso-8859-1, iso-8859-2, cp-1250 etc.
Standard Python strings (not unicode strings) use 8-bit encodings, and therefore one character can have a values between 0-255. To convert between a character and its code, Python uses two functions ord and chr.
Expand/Shrink
Zdroj: (strenc1-1.py)
  1   text = "ASCII reserves the first 32 codes (numbers 0-31 decimal) for control characters."
  2   
  3   ascii = []
  4   for ch in text:
  5       print "%3d = %s" % (ord( ch), ch)
  6       ascii.append( ord( ch))
  7   
  8   print ascii
stdout:
 65 = A
 83 = S
 67 = C
 73 = I
 73 = I
 32 =  
114 = r
101 = e
115 = s
101 = e
114 = r
118 = v
101 = e
115 = s
 32 =  
116 = t
104 = h
101 = e
 32 =  
102 = f
105 = i
114 = r
115 = s
116 = t
 32 =  
 51 = 3
 50 = 2
 32 =  
 99 = c
111 = o
100 = d
101 = e
115 = s
 32 =  
 40 = (
110 = n
117 = u
109 = m
 98 = b
101 = e
114 = r
115 = s
 32 =  
 48 = 0
 45 = -
 51 = 3
 49 = 1
 32 =  
100 = d
101 = e
 99 = c
105 = i
109 = m
 97 = a
108 = l
 41 = )
 32 =  
102 = f
111 = o
114 = r
 32 =  
 99 = c
111 = o
110 = n
116 = t
114 = r
111 = o
108 = l
 32 =  
 99 = c
104 = h
 97 = a
114 = r
 97 = a
 99 = c
116 = t
101 = e
114 = r
115 = s
 46 = .
[65, 83, 67, 73, 73, 32, 114, 101, 115, 101, 114, 118, 101, 115, 32, 116, 104, 101, 32, 102, 105, 114, 115, 116, 32, 51, 50, 32, 99, 111, 100, 101, 115, 32, 40, 110, 117, 109, 98, 101, 114, 115, 32, 48, 45, 51, 49, 32, 100, 101, 99, 105, 109, 97, 108, 41, 32, 102, 111, 114, 32, 99, 111, 110, 116, 114, 111, 108, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114, 115, 46]
Doba běhu: 19.4 ms
Expand/Shrink
Zdroj: (strenc1-2.py)
  1   codes = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  2   
  3   for code in codes:
  4       print chr( code),
stdout:
h e l l o   w o r l d
Doba běhu: 19.9 ms