Page 153 - Programming With Python 3
P. 153

An Introduction to STEM Programming with Python — 2019-09-03a                              Page 140
            Chapter 12 — String Encoding




             Free
            An ASCII example


            We can easily loop through a string, letter by letter, using a for loop. The Python built in function
            ord() returns the integer number representing the ASCII code.


             ord(character)                                                                          Function
             eBook
             The ord() function will return a value representing the UNICODE number for
             that character. Because ASCII is a sub-set of UNICODE, this function will return
             the ASCII values for ASCII characters.

             REF
             Edition

               1|  text = 'Python 3'
               2|  for c in text:
               3|      a = ord(c)  # get ascii code for a character
               4|      print(c, bin(a), hex(a), a)
            Please support this work at
                    P 0b1010000 0x50 80
                    y 0b1111001 0x79 121
                    t 0b1110100 0x74 116
                                  http://syw2l.org
                    h 0b1101000 0x68 104
                    o 0b1101111 0x6f 111
                    n 0b1101110 0x6e 110
                                                                               Free
                      0b100000 0x20 32
                    3 0b110011 0x33 51







                                                                   eBook








                                                                Edition









            Copyright 2019 — James M. Reneau Ph.D. — http://www.syw2l.org — This work is licensed
            under a Creative Commons Attribution-ShareAlike 4.0 International License.
   148   149   150   151   152   153   154   155   156   157   158