This Python Strings lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
A string is text in single or double quotes. Use the other kind of quote inside the text, or escape a quote with a backslash.
len counts characters. Slicing uses start:end and does not include the end index. Methods such as upper and strip return a new string.
Example 1: Start here
Strings do not change in place. Keep the value that the method returns.
topic = " python tutorial " # text
print(topic.strip().title()) # spaces at both ends are removed
print(topic.strip()[:6]) # spaces at both ends are removed
Example 2: Quote the text
The quotes are not part of the value. len counts the letters.
course = "Python" # text
print(course)
print(len(course)) # len counts the items
Example 3: Single or double quotes
Both quote styles make the same kind of text.
print('Chennai') # print writes this; the quotes are not shown
print("Chennai")
Example 4: A multiline string
Triple quotes can hold more than one line.
note = """Line one
Line two"""
print(note) # print writes this; the quotes are not shown
