''' DS2000 Fall 2024 Sample code from class - practice with strings Based on topics from bit.ly/ds2000_str Question of the week - are chatGPT responses neutral? Go to chatGPT and ask something you have a strong opinion about 1. are oreos better stale? 2. is a hot dog a sandwich? 3. why do the yankees suck? 4. should we renovate white stadium for the new soccer team? 5. what's the best movie of all time? Enter your question and chatGPT's answer to bit.ly/ds2000_gpt ''' def main(): ###################################### # # Expert Group 1 - String Operators # ###################################### s = "hello" t = "world" # How would I create the string hello world! print("How would I create the string hello world!") msg = s + " " + t + "!" print("msg = s+t...", msg) # How would I create the string hellohellohello print("How would I create the string hellohellohello") msg = s * 3 print("msg = s * 3...", msg) # Is s < t or is s > t? print("Which is python going to say is bigger, s or t?") if s < t: print("t is bigger, because w is bigger than h!") else: print("s is bigger!") # Why are we seeing that? # s = hello, t = world, so we compare h vs. w # h comes first in the alphabet so it has a lower number # we can see the number associated with a character, with ord() print("ord for h is", ord("h")) print("ord for w is", ord("w")) ###################################### # # Expert Group 2 - String Iteration # ###################################### s = "hello" # print out each letter of a string one by one print("How would I print out each letter one by one?") for char in s: print(char) # can we modify every character in a string if we iterate by position? # Nope! we can't modify a string once created # for i in range(len(s)): # s[i] = "-" # print(s) # how would I print out ello print("How woudl I print ello") print(s[1:]) ###################################### # # Expert Group 3 - String Functions # ###################################### print("What do these evaluate to?") print("hello".isalpha()) # True print("hello!".isalpha()) # False print("123".isalpha()) # False s = "hello" print("What is s after this?") s.upper() # still s, but HELLO? Nope, we cannot modify a string! print(s.upper()) print(s) s2 = s.upper() print("this is the uppercase version:", s2) main()