We’ve already seen how powerful Python can be when dealing with strings. We can search the string with the in keyword, we can take a piece of text by slicing it, take its length, format with f-strings, etc. That’s actually only the tip of the iceberg.
In Python, there are many utility functions to deal with more involved text manipulations. There are 40+ utilities that can be used in different scenarios. The whole list of those methods can be found on the official python webpage: string methods. The good news is that it’s not necessary to remember them all. Almost all of them have a very intuitive name and it’s easy to find what’s necessary with a quick google search. We will cover some very popular functions which come in handy from time to time and are used frequently when working with strings.
# .upper() makes all the letters uppercase
s = 'What happened to Anna?'
up = s.upper()
print('Hi 123 this is Sally!'.upper()) # HI 123 THIS IS SALLY!
print(s) # What happened to Anna?
print(s.upper()) # WHAT HAPPENED TO ANNA?
print(up) # WHAT HAPPENED TO ANNA?
# .lower() makes all the letters lowercase
s = 'What happened to Anna?'
low = s.lower()
print('Hi 123 this is Sally!'.lower()) # hi 123 this is sally!
print(s) # What happened to Anna?
print(s.lower()) # what happened to anna?
print(low) # what happened to anna?
# .title() start all the words with a capital letter (title-case)
s = 'What happened to Anna?'
title = s.title()
print('Hi 123 this is Sally!'.title()) # Hi 123 This Is Sally!
print(s) # What happened to Anna?
print(s.title()) # What Happened To Anna?
print(title) # What Happened To Anna?
# .capitalize() start all the sentences with a capital letter
s = 'What happened to Anna?'
capital = s.capitalize()
print('Hi 123 this is Sally!'.capitalize()) # Hi 123 this is sally!
print(s) # What happened to Anna?
print(s.capitalize()) # What happened to anna?
print(capital) # What happened to anna?
# .swapcase() turn lowercase -> uppercase, uppercase -> lowercase
s = 'What happened to Anna?'
swapped = s.swapcase()
print('Hi 123 this is Sally!'.swapcase()) # hI 123 THIS IS sALLY!
print(s) # What happened to Anna?
print(s.swapcase()) # wHAT HAPPENED TO aNNA?
print(swapped) # wHAT HAPPENED TO aNNA?
It’s important to note that the string methods do not modify the original variable. They create a new value and return it. The original value stays the same. So, when calling a.lower(), the value of a does not change. It stays the same, but lower() returns a new value that contains the lowercase version of a.
Challenge
You are given a command and a piece of text. Your task is to apply that command to the text.
The command can be any of: upper, lower, capitalize, title, swap.
The command can also be represented with capitals and lowercase letters. The command handling should not be case-sensitive.
The first line of the program contains the command. The second line contains the text which needs to be modified.
The output should be a single line - the transformed text.