Member-only story
Hi everyone, welcome back. On this cheat sheet, I will show how to perform basic tasks involving strings in Python. A string can be identified in Python by using quotation marks. Both single quote and double quotes can be used to create a string. For example, ‘python’ is a string and “python” is also a string. We will go over many string related functions in this cheat sheet on how strings can be manipulated for programming use.
Use the print function to display your string output to the console:
print(‘String’)Output: String
Set a string to a variable and check the length of the string:
stringVariable = ‘String’
print(len(stringVariable))Output: 6
Remove whitespace in a string:
stringVariable = ‘ String ’
print(stringVariable.strip())Output: String
Make all characters in the string uppercase:
stringVariable = ‘String’
print(stringVariable.upper())Output: STRING
Make all characters in the string lowercase:
stringVariable = ‘String’
print(stringVariable.lower())Output: string
Replace a value within a string, in this example this will replace ‘S’ with ‘Q’:
stringVariable = 'String'
print(stringVariable.replace('S'…