Python - 18

Python - 18: Working With Strings

Strings have built-in methods that let you change, search, and work with text.

Your challenge: Change the letter case, replace part of a string, count its characters, and check whether some text exists inside it.

✦ Python Editor
▶ Output

Change letter case

Use upper() to create an uppercase version and lower() to create a lowercase version.

For example: message.upper()

Replace text

Use replace() to create a new string with part of the text changed: message.replace("awesome", "fun")

Count characters

Use len(message) to count all characters in the string, including spaces.

Search inside a string

Use in to check whether text exists: if "python" in message:

Try it: Change message and test whether a different word appears inside it.

Good Python habit: String methods like upper() and replace() return new strings. They do not change the original variable unless you assign the result back to it.

👀 Show Example Solution
message = "python is awesome"

print(message)

print(message.upper())

print(message.lower())

print(message.replace("awesome", "fun"))

print(len(message))

if "python" in message:
    print("Python was found!")

The string methods create changed versions of the text, while len() counts characters and in checks whether a piece of text is present.