You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Strings are one of the most commonly used data types in Python. A string is a sequence of characters enclosed in quotes. Python provides a rich set of methods for working with text — searching, slicing, formatting, and transforming.
# Single quotes
greeting = 'Hello, World!'
# Double quotes
message = "Python is fun!"
# Triple quotes — multi-line strings
paragraph = """This is a
multi-line string that
spans three lines."""
# Triple single quotes also work
another = '''Same thing
with single quotes.'''
print(greeting)
print(paragraph)
Use double quotes when your string contains an apostrophe, and single quotes when it contains double quotes:
sentence = "It's a beautiful day."
html = '<div class="container">Hello</div>'
Every character in a string has a position (index), starting from 0:
word = "Python"
# P y t h o n
# 0 1 2 3 4 5
# -6-5-4-3-2-1
print(word[0]) # P — first character
print(word[5]) # n — last character
print(word[-1]) # n — last character (negative indexing)
print(word[-2]) # o — second-to-last
Extract a portion of a string using [start:stop:step]:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.