I do not think it has a specific function for that. But as a good self-respecting language, it has two functions to work with this type of problem.
The first function is to
.upper () which returns the uppercase character and the second character is
.lower () which does the opposite. Technically, they do not treat the character to ignore or restore the state, but we have other functions that perform the verification.
To check if a character is already in uppercase, just use the function
.isupper () and the reverse .islower () & # 39 ;.
Let's create the function
chaîne_réver which takes an argument as a string, it must return "the inverse of the string" (in upper case). Basically, we will browse each character by checking if it is capitalized, if it is, let's make it lowercase and vice versa.
The applied algorithm is as follows:
def reverse (string): back = & # 39; & # 39; for the character in the chain: if character.isupper (): returns + = characterlow () if not: returns + = character.upper () back back
This is basically the code. The question is: the code is too big! In this case, we can use a very useful function in Python (which must also appear in some languages), the function
map (). This function maps each element of an object that can be iterated (such as strings, tuples, lists, dictionaries) and returns a final result by performing a function on each element.
In this case, the return string function would have become simply this:
def reverse (string): return & # 39; & # 39; .join (map (lambda c: c.upper () if c.islower () otherwise c.lower (), string))
Was the code still confusing?
The map function takes a function and a string, and parses each character using the function of the first argument (which uses the same algorithm as the previous function), and returns a cartographic object, which looks like an iterator.
What does it mean?
It means that I have to join the characters with a
rejoin, so he was there. Otherwise, I will have a list, instead of a string in return.
That's it …