Last active
July 2, 2017 18:51
-
-
Save bubbobne/ed3a7cdb71502d873888e8913a7ae239 to your computer and use it in GitHub Desktop.
Example to use of string method in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A collection of method to manipulate string object in python. | |
The str object is a sequence, other example of sequence are: bytes, bytesarray, list, tuple, range | |
''' | |
def loToUpAndUpToLo(inputString): | |
''' | |
Switch the lowercase character to uppercase and uppercase to lowercase. | |
Create a list as a copy of the string and then iterate into character to check and transformate it. | |
Args: | |
inputString(str): The string to convert. | |
Returns: | |
str: The return value. | |
''' | |
stringAsList=list(inputString) | |
for i,c in enumerate(stringAsList): | |
if c.islower(): | |
stringAsList[i]=c.upper() | |
elif c.isupper(): | |
stringAsList[i]=c.lower() | |
return "".join(stringAsList) | |
stringToTest="tEsT" | |
print("Try function to swap case, it works? ",stringToTest.swapcase()==loToUpAndUpToLo(stringToTest)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment