Previous Lecture Lecture 16 Next Lecture

Lecture 16, Thu 02/29

More on Strings cont.

Recorded Lecture: 2_29_24

String methods cont.

s = "how much wood can a woodchuck chuck"

print(s.find("wood")) # 9
print(s.find("wood", 13)) # 20
print(s.rfind("wood")) # 20
print(s.find("WOOD")) # -1
s = "how much wood can a woodchuck chuck"

print(s.count("wood"))
print(s.count("w"))
print(s.count("z"))
s1 = "it's a lovely day"
s2 = "\n\t   Hello!\n\n "
print(s2.strip()) # Hello!

String Comparison (Lexicographical Order)

s1 = "abcd" 
s2 = "aeiou"
print(s1 < s2) # True
print(s1 > s2) # False
print(s1 == s2) # False
s1 = "abcd" 
s2 = "abc"
print(s1 < s2) # False
print(s1 > s2) # True
print(s1 == s2) # False

s1 = "ABC"
s2 = "abc"
print(s1 < s2) # True
print(s1 > s2) # False
print(s1 == s2) # False

print(s2.upper()) # ABC
print(s1.lower()) #abc
print(s1.upper() < s2.upper()) # False
print(s1.upper() > s2.upper()) # False
print(s1.upper() == s2.upper()) # True

s3 = "ant"
s4 = "Apple"

print(s3 < s4) # True
print(s3 > s4) # False
print(s3 == s4) # False
print(s3.lower() < s4.lower()) # False
print(s3.upper() > s4.upper()) # True 
print(s3.upper() == s4.upper()) # False

Boolean String Methods

alpha = "ABCD"
num = "1234"
mixed = "AB12 ?!"
whitespaces = "\n \t"

print(alpha.isalnum()) # True
print(mixed.isalnum()) # False
print(alpha.islower()) # False
print(mixed.isupper()) # True
print(whitespaces.isspace()) # True
print(mixed.isspace()) # False
print(alpha.startswith("AB")) # True
print(mixed.endswith("!")) # True

split and join methods

s = "This is a sentence"
print(s.split()) # ['This', 'is', 'a', 'sentence']
print(s) # This is a sentence

# csv (or comma separated values)
s = "First,Last,CS8,100,90"
print(s.split(",")) # ['First', 'Last', 'CS8', '100', '90']

s = "Mississippi"
print(s.split("is")) # ['M', 's', 'sippi']
s = "Mississippi"
splitList = s.split("is")
print(splitList) # ['M', 's', 'sippi']
newString = "IS".join(splitList)
print(newString) # MISsISsippi