A string is also a sequence. It is made of ordered characters, so it can be indexed, sliced, and processed with many string methods.
String methods
Common string methods include:
lower(): convert to lowercase.upper(): convert to uppercase.capitalize(): capitalize the first letter.strip(): remove leading and trailing whitespace.replace(): replace part of a string.
s = "Hello World!"
print(s.lower())
print(s.upper())
print(s.capitalize())
print(" Hello World!\n \t".strip())
print(s.replace("Hello", "Bye"))String methods usually return a new string instead of modifying the original string.
INTERACTIVE LAB
String method studio
ACCESSFREE
Pick a method and watch whitespace, tokens, and the returned value separately. The original string never changes.
hello Python lab
KNOWLEDGE QUIZ
QUESTION
What does s.strip() mainly do?
split() and join()
split() breaks a string into a list. join() combines strings from a list.
date_time = "2026/1/14 8:26:51"
date_part, time_part = date_time.split(" ")
year, month, day = date_part.split("/")
hour, minute, second = time_part.split(":")
print("Date parts: year=%s, month=%s, day=%s" % (year, month, day))
print("Time parts: hour=%s, minute=%s, second=%s" % (hour, minute, second))
new_date = "-".join([day, month, year])
new_time = hour + "h" + minute + "m" + second + "s"
print("Reformatted: %s %s" % (new_date, new_time))Output:
Date parts: year=2026, month=1, day=14
Time parts: hour=8, minute=26, second=51
Reformatted: 14-1-2026 8h26m51ssplit() and join() are common in text, date, path, and CSV processing. This example splits the date and time into six parts, then rebuilds them into a visibly different display format.
KNOWLEDGE QUIZ
QUESTION
What type is the result of "a,b,c".split(",")?
HANDS-ON PRACTICE
Rotate a string left by k characters
Read a string and integer k, then rotate the string left by k characters.
- Read string s.
- Read k and normalize it to the string length.
- Use slicing to build the rotated result.