Skip to content
  • Characters, ASCII & DIY String Functions · Data Science

Characters, ASCII & DIY String Functions

LearnViaNotes @learnvianotes· 2 min read

Every character is a number. Knowing the codes lets you build string functions yourself — a common interview exercise and the basis of text encoding.

ord() and chr()

Python
text = "The quick brown fox"
print([ord(c) for c in "Tq "])                    # [84, 113, 32]
print(chr(97), chr(65), chr(93))                  # a A ]
codes = [ord(c) for c in text]
print("".join(chr(n) for n in codes) == text)     # True
print("".join(chr(n) for n in range(65, 91)))     # ABCDEFGHIJKLMNOPQRSTUVWXYZ
print("".join(chr(n) for n in range(90, 64, -1))) # ZYXWVUTSRQPONMLKJIHGFEDCBA
RangeCharacters
48–570–9
65–90A–Z
97–122a–z (each lowercase letter = its uppercase + 32)
32space

DIY upper, lower and title

Python
def upper(text):
    return "".join(chr(ord(c) - 32) if "a" <= c <= "z" else c for c in text)

def lower(text):
    return "".join(chr(ord(c) + 32) if "A" <= c <= "Z" else c for c in text)

def title(text):
    return " ".join(upper(w[:1]) + lower(w[1:]) for w in text.split(" "))

print(upper("delhi"), lower("CARTOON"))               # DELHI cartoon
print(title("let us study machine learning today"))   # Let Us Study Machine Learning Today
print(title("hello  world"))                          # Hello  World

DIY isalpha and isdigit

Python
def is_alpha(text):
    return len(text) > 0 and all("A" <= c <= "Z" or "a" <= c <= "z" for c in text)

def is_digit(text):
    return len(text) > 0 and all("0" <= c <= "9" for c in text)

print(is_alpha("datascience"), is_alpha("1234"))     # True False
print(is_digit("1234"), is_digit("machinelearning")) # True False

Finding runs of digits and words

Python
txt = "The number is 8372493 not 423423"
runs, current = [], ""
for ch in txt:
    if ch.isdigit():
        current += ch
    elif current:
        runs.append(current)
        current = ""
if current:
    runs.append(current)
print(runs)                                     # ['8372493', '423423']

sentence = "GFG is launching a new Data Structure in Python Course"
print([w for w in sentence.split() if w.lower().startswith("i")])   # ['is', 'in']
print([w for w in sentence.split() if w.lower().endswith("e")])     # ['Structure', 'Course']
print("let us study data science".find("data"))                     # 13

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report