banner

© 2004-21 ict4u.net

String Content Tests

These methods will not only tell you if a string contains only letters, only numbers or letters and numbers but also if it is lowercase, uppercase or title case

# Author: A Knifton
# Date:
# Purpose:Demo of Python methods to test a string

print(f"{'=' * 6} Using Python String Test Mehtods {'=' * 6}")

myText = "YouTuberFappletuploadedamontageparodyvideo"

# Testing for only a-z:
print(f"\n\nTest String: {myText}")
print(f"Alphabetic Test (A-z): {myText.isalpha()}")
print(f"Alpha and Numeric Test (A-z, 0-9): {myText.isalnum()}")
print(f"Numeric Test (0-9): {myText.isnumeric()}")

# format testing
myText2 = "YouTuber Fapplet uploaded a montage parody video"
myText3 = myText2.title()
print(f"\n\nTest String: {myText3}")
print(f"Lower Case Test (a-z): {myText3.islower()}")
print(f"Title Case Test (each word capitalised): {myText3.istitle()}")
print(f"Upper Case Test (A-Z): {myText3.isupper()}")
print(f"Space Test (Space or tab character): {myText3.isspace()}")

# Testing for only numbers
myNumStr = "34"
print(f"\n\nTest String: {myNumStr}")
print(f"Alphabetic Test (A-z): {myNumStr.isalpha()}")
print(f"Alpha and Numeric Test (A-z, 0-9): {myNumStr.isalnum()}")
print(f"Numeric Test (0-9)   {myNumStr.isnumeric()}")

myNumStr = "34D"
print(f"\n\n Test String: {myNumStr}")
print(f"Alphabetic Test (A-z): {myNumStr.isalpha()}")
print(f"Alpha and Numeric Test (A-z, 0-9): {myNumStr.isalnum()}")
print(f"Numeric Test (0-9):  {myNumStr.isnumeric()}")