Lesson 11/48 ยท ๐Ÿ Your First Python
๐Ÿ Your First PythonLesson 11/48
Phase 1 ยท Your First Python25 min

Strings, Working with Text

Master text manipulation, the most common thing programs do

A string is any sequence of characters, letters, numbers, symbols, spaces. Most real programs deal heavily with text: names, messages, URLs, files.
Key string operationspython
text = "Hello, World!"

print(len(text))           # Length: 13
print(text.upper())        # HELLO, WORLD!
print(text.lower())        # hello, world!
print(text.replace("World", "Python"))  # Hello, Python!
print(text[0])             # H  (first character)
print(text[-1])            # !  (last character)
print(text[0:5])           # Hello  (slice)
String indexing starts at 0. The first character is at position 0, the second at 1, etc. text[0:5] means "characters from position 0 up to (not including) 5".
Checking and splitting stringspython
email = "alice@example.com"

print("@" in email)          # True, checks if @ is in the string
print(email.startswith("alice"))  # True
print(email.endswith(".com"))     # True
print(email.split("@"))      # ['alice', 'example.com']
print(email.strip())         # removes whitespace from both ends
๐ŸŒIn the Real World...

Log files are strings. URLs are strings. YAML configs are strings. 90% of DevOps automation is string manipulation, parsing log lines, building API URLs, formatting config values.

Practice Exercises

0/3 solved
Exercise 1 of 3easy
โฑ 00:00

Reverse a String

Write a function reverse_string(s) that returns the string reversed.
Example: reverse_string("hello") โ†’ "olleh"
solution.py
1 / 3
Exercise 2 of 3easy
โฑ 00:00

Count Vowels

Write a function count_vowels(s) that returns the number of vowels (a, e, i, o, u) in the string. Case-insensitive.
solution.py
2 / 3
Exercise 3 of 3medium
โฑ 00:00

Is a Palindrome?

Write a function is_palindrome(s) that returns True if the string reads the same forwards and backwards, ignoring case and spaces.
Examples: "racecar" โ†’ True, "hello" โ†’ False, "A man a plan a canal Panama" โ†’ True
solution.py
3 / 3
Solve all 3 exercises to unlock completion