7 Powerful Ways to Uppercase a Python String (My Surprisingly Simple Guide for Beginners 🤯)

Key Highlights

Uppercase a Python String
Uppercase a Python String

 

  • Find out what the Python upper() function does, and just how useful it can be.
  • Learn how you can uppercase text even if you want to avoid using built-in Python methods.
  • It’s filled with real examples and some funny stories from my days of learning to code.
  • It’s written in a way that’s simple for beginners to understand and still provides technical information for more advanced programmers.
  • It’s perfect for people learning the basics of Python or understanding ASCII & Unicode.
  • It even contains 7 practical techniques and one of my favorite Unicode tricks.
  • It contains internal and external links to enrich your understanding of the topic.

If You’re Searching for “How Do I Uppercase a Python String Without Using Built-ins…” You’re in the Right Place 👍

If you’ve been searching for how to uppercase strings in Python, it’s likely for one of two reasons:

But that’s the boring version.

I typed:

print("hello".upper())

And boom — HELLO. It felt like cheating. 😂

If no, it leaves it alone

It’s simple. It’s elegant. It works every time.

That’s where the fun begins.

⭐ 1.

name = "python explorer"
print(name.upper())

Output:

PYTHON EXPLORER

⭐ 2.

I built two lists:

  • One for lowercase letters
  • One for uppercase letters

Like this:

lowercase = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
uppercase = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

And I would map every lower case to its uppercase counterpart.

for char in word:
    if char in lowercase:
        index = lowercase.index(char)
        result += uppercase[index]
    else:
        result += char

⭐ 3.

Here’s the cool part:

  • ‘a’ → ASCII 97
  • ‘z’ → ASCII 122
  • ‘A’ → ASCII 65
  • ‘Z’ → ASCII 90

The difference is 32.

Uppercase without Built-ins in ASCII
Uppercase without Built-ins in ASCII

uppercase_code = ord(char) - 32
uppercase_letter = chr(uppercase_code)

Example:

word = "python"
result = ""

for char in word:
    if 97 <= ord(char) <= 122:
        result += chr(ord(char) - 32)
    else:
        result += char

That’s it.

⭐ 4.

Unicode covers:

  • French
  • Chinese
  • Hindi
  • Emojis (yes, emojis 😄)

if 'a' <= char <= 'z':
    result += chr(ord(char) - 32)

⭐ 5.

At some point, I realized:

It might look cleaner.”

Example:

mapping = {chr(i): chr(i - 32) for i in range(97, 123)}

Then:

if char in mapping:
    result += mapping[char]

It’s great for clarity.

⭐ 6.

So I wrote:

def my_upper(word):
    result = ""
    for char in word:
        if 'a' <= char <= 'z':
            result += chr(ord(char) - 32)
        else:
            result += char
    return result

This is the nearest Python alternative of DIY upper.

⭐ 7.

result = ''.join(chr(ord(c)-32) if 'a' <= c <= 'z' else c for c in word)

cryptography encoding / decoding text processing building compilers handling multilingual data

 

Final Thoughts

✔ Python upper() is simple

Related Reads:

Previous Article

Tokens in C Programming: The 7 Secrets That Made C Easier for Me

Next Article

Types of Network Protocols and Their Uses

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨