4.2 Testing for substrings
Use not
and not
in
to see if the content of one string is within another.
"Quantum" in "Quantum computing"
True
"QUANTUM" not in "Quantum computing"
True
To test without worrying about uppercase and lowercase, we use the casefold method on each string first.
"QUANTUM".casefold() in "Quantum computing".casefold()
True
Python provides upper and lower to change the case of strings.
"Charles Darwin".upper()
'CHARLES DARWIN'
"CAUTION: HELMETS MUST BE WORN".lower()
'caution: helmets must be worn'
Use capitalize to put the first character in uppercase and the remaining ones in lowercase. Note that this may not give you what you want if the text contains characters that should remain in uppercase.
"this NEEDS to look like A Sentence!"...