String Handling
Let’s Play with Words! ✨
Strings are like the words and sentences of programming—they're everywhere! And just like words, you can measure them, chop them up, shout them in uppercase, or whisper in lowercase. Let’s explore the magical world of string handling with some fun examples! 🧙♂️
LENGTH: How Long Is Your String? 📏
Have you ever wondered how long a sentence is? The LENGTH function is here to tell you. In pseudo-code, it’s as simple as:
DECLARE MyString : STRING
MyString ← "Programming is fun!"
OUTPUT LENGTH(MyString)
In Python, you’d do this:
MyString = "Programming is fun!"
print(len(MyString))
Output:
19 (Yes, spaces count too!)
Fun Challenge 🎉:
- Try this:
LENGTH("Supercalifragilisticexpialidocious") - Write a Python program to ask the user for their name and print its length. What’s the length of your name? 👀
SUBSTRING: Cutting the Cake 🎂
Let’s say you’ve got a long word, but you just want a slice of it. That’s where SUBSTRING comes in handy!
Syntax in Pseudo-Code:
DECLARE MyString : STRING
MyString ← "Docusaurus is awesome!"
OUTPUT SUBSTRING(MyString, 1, 9)
Translation to Python:
MyString = "Docusaurus is awesome!"
print(MyString[0:9]) # Remember, Python starts counting from 0!
Output:
Docusaur
Another Example 🎯:
Want the last 7 letters of "Superhero"?
MyString = "Superhero"
print(MyString[-7:]) # This grabs the last 7 characters
Output:
perhero
Fun Challenge 🎉:
- Extract the word "awesome" from the sentence:
"Learning to code is awesome and fun!"
UCASE: SHOUT IT OUT LOUD! 📢
Sometimes, you need to shout. Whether you’re excited or just want to emphasise something, UCASE turns all your letters into uppercase.
Pseudo-Code:
DECLARE Greeting : STRING
Greeting ← "hello, world!"
OUTPUT UCASE(Greeting)
Python Translation:
Greeting = "hello, world!"
print(Greeting.upper())
Output:
HELLO, WORLD!
Fun with UCASE:
Excitement = "coding is the best!"
print(Excitement.upper())
Output:
CODING IS THE BEST!
LCASE: Whisper Softly... 🤫
If shouting is too loud for your taste, how about whispering? LCASE transforms everything into lowercase, perfect for a gentle tone.
Pseudo-Code:
DECLARE Announcement : STRING
Announcement ← "PYTHON IS AWESOME!"
OUTPUT LCASE(Announcement)
Python Translation:
Announcement = "PYTHON IS AWESOME!"
print(Announcement.lower())
Output:
python is awesome!
Real-Life Example 🌟:
If you’re working with user input, you might want to standardise the case:
UserInput = input("Type Yes or No: ")
if UserInput.lower() == "yes":
print("Great! Let’s move on.")
Putting It All Together 🎉
Let’s combine all these string-handling tricks into one awesome program. Here’s an example to play with:
Pseudo-Code:
DECLARE Sentence : STRING
Sentence ← "Coding is magical!"
OUTPUT "Length: ", LENGTH(Sentence)
OUTPUT "Uppercase: ", UCASE(Sentence)
OUTPUT "Lowercase: ", LCASE(Sentence)
OUTPUT "Substring: ", SUBSTRING(Sentence, 8, 7)
Python Translation:
Sentence = "Coding is magical!"
print("Length:", len(Sentence))
print("Uppercase:", Sentence.upper())
print("Lowercase:", Sentence.lower())
print("Substring:", Sentence[7:14])
Output:
Length: 18
Uppercase: CODING IS MAGICAL!
Lowercase: coding is magical!
Substring: is magi
A Fun Challenge for You! 🧩
- Ask the user to enter their favourite quote.
- Print:
- The length of the quote.
- The quote in uppercase and lowercase.
- A substring of the first 5 characters.
- Add a funny comment if the length of the quote is more than 50 characters, like, “Wow, that’s a long quote! Are you writing a novel?” 😄
With string handling, you now have the power to bend words to your will. Go ahead, write magical programs, and make your strings dance! 🪄✨