message = "Hello, world!" print(message) # Output: "Hello, world!"
Hello, world!
print(message[0]) # Output: "H" print(message[7]) # Output: "w"
H w
print(len(message)) # Output: 13 (length of the string) print(message.upper()) # Output: "HELLO, WORLD!" (converts to uppercase) print(message.replace("Hello", "Hi")) # Output: "Hi, world!" (replaces a substring) print(message.split(",")) # Output: ["Hello", " world!"] (splits the string into a list)
13 HELLO, WORLD! Hi, world! ['Hello', ' world!']
message = "Hello?" print(message[0]) # Outputs: H message[0] = "J" # This results in an error
H
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 3 1 message = "Hello?" 2 print(message[0]) # Outputs: H ----> 3 message[0] = "J" # This results in an error TypeError: 'str' object does not support item assignment
greeting = "Hello" name = "John" message = greeting + " " + name print(message) # Output: "Hello John"
Hello John