Introduction to Python Strings
- Python strings are sequences of characters used to store text. They are one of the most commonly used data types in Python. Strings can be defined using single quotes (''), double quotes (""), or triple quotes (''' ''' / """ """) for multi-line text.
Strings in Python
A string in Python is a sequence of characters used to store and represent text.
Strings are written inside quotation marks (single, double, or triple quotes).
Creating a String Variable
This example shows how to define and print a string in Python.
message = "Welcome to Python"
print(message)
Quotation Marks in Python Strings
Python allows strings to be created using:
Single quotes (' ')
Double quotes (" ")
Both work the same way, but double quotes are often used when the string contains an apostrophe.
Using Single and Double Quotes
This example demonstrates strings created with single and double quotation marks.
name = 'Python'
language = "Python Programming"
print(name)
print(language)
Printing a String
This code stores a text message in a variable and prints it to the console using the print() function.
text = "Python is easy to learn"
print(text)
Multi-Line Strings in Python
A multi-line string allows text to span across multiple lines.
It is created using triple single quotes (''' ''') or triple double quotes (""" """).
Multi-Line String
This example shows how to write and print a multi-line string.
message = """Python is a powerful language.
It is easy to learn.
It is widely used in web development."""
print(message)
Multi-Line String Using Single Quotes
This example demonstrates a multi-line string using triple single quotes.
info = '''Python supports
multiple programming styles
and is beginner friendly.'''
print(info)
Strings store text and are defined using single ' ' or double " " quotes , Multi-line strings use triple quotes ''' ''' or """ """ , Use print() to display strings; they are immutable and support concatenation.