Number / String Conversions

 

Python Number to String conversion functions:

Python String to Number to String converstion functions:

Let's convert a string to an integer ourselves:

def str2int(s):
  # we are assuming that the string contains a valid integer, errors will result otherwise
  digits = '0123456789'
  answer = 0
  s = s.strip()    # remove any leading and trailing spaces
  multiplier = 1   # multiply by 1, then 10, then 100, etc. 
  for i in range(-1, -len(s)-1, -1):  # work backwards from the last digit to the first
    dig = s[i]
    dig2int = digits.find(dig)
    answer += dig2int * multiplier
    multiplier *= 10
  return answer

Now let's convert an integer into its string representation

def int2str(n):
  # assuming a non-negative integer
  answer = ""
  digits = "0123456789"
  while n > 0:
    ones_digit = n % 10
    a_string_digit = digits[ones_digit]
    answer = a_string_digit + answer
    n //= 10
  if answer == "":
    answer = "0"
  return answer

The underlying code for characters -- ASCII code.

ord(c):  returns the code used to store the character c in memory.  Examples:  ord("A") -> 65, ord("a") -> 97, ord(" ") -> 32. Try to find the code for other characters, like the character "3" or the character "$",  Every character on the keyboard that is stored in a document that you save has an ASCII code, including "\n" (the Enter or NewLine character).

chr(n): This converts a number (like 65) into its corresponding character.  Examples: chr(65) -> "A", chr(43) -> "+", chr(55) -> "7"

Find an ASCII code table (or chart), and see all of the codes  Note that the codes for the upper-case letters are in the range (65-90) and lower-case (97-122), and the digits (48-57). 

Exercises:

  1. int2bin(n): Convert a non-negative integer into its string representation in base 2.  Examples: int2bin(0) -> "0", int2bin(2) -> "10", int2bin(6) -> "110"

  2. binstr2int(s): Do the inverse of problem #1: convert a binary string representation of a number into a number.  Examples: binstr2int("10") -> 2, binstr2int("1001") -> 9

  3. (a little challenge): Another version of str2int(s):  The version of str2int() shown  above goes through the string characters from back to front.  Write a version of str2int(s) that goes through the characters from front to back (don't game this problem by simply reversing the string first).

  4. (more challenge): int2Roman(n) takes an integer in the range 1-89 and returns a string with the Roman Numeral version.  Examples: int2Roman(6) -> "VI"  int2Roman(9) -> "IX"  int2Roman(62) -> "LXII"  Yes, you could write 89  statements like: if i == 6: return "VI", but try to do something shorter.
  5. (even morer challenge) str2float(s): You'll be given a string containing a valid floating point number (like "-54.69004"), convert it into a floating point number.  The string will not be in scientific notation (no "e").
  6. isLetter(c): given a string of a single character, will return True if the c is an upper-case or lower-case letter, False otherwise.  Examples: isLetter("f") -> True, isLetter("5") -> False.  You MUST use only ord() and/or chr().  You may not use .find(), or any other built-in Python function.
  7. mirror(s): given a word with only lower-case letters, "mirror" the word.  Examples: mirror("abc") -> "zyx", mirror("zyxw") -> "abcd", mirror("lmno") -> "onml".