Strings (str)
A string is a sequence of characters, used to store text information. Strings must be enclosed in quotes.
Defining Strings
Python allows single quotes (') or double quotes ("). It is best practice to stick to one type consistently, but they are interchangeable.
python name_single = 'John' name_double = "Doe"
This allows you to include quotes within the string without escaping:
sentence = "He said, 'Hello!'"
Multi-line Strings
For strings that span multiple lines, use triple quotes (either single or double).
python story = """Chapter 1: The hero awoke to the sound of distant drums. """ print(story)
Finding the Length of a String (len())
The built-in len() function returns the number of characters in a string.
python text = 'Python' length = len(text) print(length) # Output: 6