- Home
- /
- Python Strings
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python Strings
Python Strings
What Are Strings?
In Python, any text enclosed in single ('
) or double ("
) quotes is treated as a string. A string is a sequence of characters, like words or sentences. It can hold letters, numbers, symbols, or even empty space.
name = "Arshyan"
print("Welcome, " + name)
Output:
Welcome, Arshyan
It doesn’t matter whether you use single or double quotes. Both work the same:
message1 = 'Learning Python'
message2 = "Learning Python"
print(message1 == message2) # True
Strings Inside Strings
If your sentence includes quotation marks, you must be careful with how you format it.
Incorrect (Causes Error)
print("He said, "I love coding."")
This results in a SyntaxError
, because Python gets confused about where your string starts and ends.
Correct Ways
Use single quotes outside:
print('He said, "I love coding."')
Or escape double quotes with backslash:
print("He said, \"I love coding.\"")
Output:
He said, "I love coding."
He said, "I love coding."
Multi-line Strings
Want to write multiple lines as a single string? Use triple quotes—either '''
or """
.
This is useful for:
Displaying formatted instructions
Returning multi-line text
Adding descriptive blocks to output
rules = """
Please follow the steps:
1. Open the application
2. Enter your details
3. Click submit
"""
print(rules)
note = '''This program was created by
Learn With Arshyan.
Keep learning and practicing.'''
print(note)
Output:
Please follow the steps:
1. Open the application
2. Enter your details
3. Click submit
This program was created by
Learn With Arshyan.
Keep learning and practicing.
Summary
Strings are created by placing text inside
' '
or" "
.Use escape characters (
\
) to include quotes inside a string.Triple quotes (
''' '''
or""" """
) allow multi-line strings.Strings are commonly used for messages, labels, user input, and storing readable text.