24. Other string things

  • There are other useful functions for checking and manipulating characters and strings
#Defines and prints original string
str = "AbCdEfG"
print(str)

#Checks to use in IF statements
print(str.isalpha())
print(str.islower())
print(str.isupper())
print(str.isdigit())

#Ways to convert strings
print(str.upper())
print(str.lower())
print(str.swapcase())
print(str.capitalize())

#Length of string
print(len(str))

Reversing a string

Sometimes you might need to reverse a string. This may be to make a working with binary strings easier, or to help create random passwords.

#This code reads the string variable,
#reverses it into the rev_string variable
rev_string = string[::-1]

Tasks

  1. Type in the code and make sure that it works
  2. Add code to reverse the string and print it out
  3. Save and label your code

Extension Task

You can combine these techniques with an IF statement to ask the user to input a letter, and print a message if the input is 1 in length, and is an uppercase letter.