We work with text all the time in programming—names, messages, addresses, usernames, and much more. In Python, text is stored using a string.
Strings are one of the most commonly used data types, so it's important to understand how they work.
What is a String?
A string is a sequence of characters used to represent text.
Strings are written inside quotes:
name = "John"
message = 'Hello, Python!'Both single (' ') and double (" ") quotes can be used.
language = "Python"is the same as:
language = 'Python'Creating Strings
You can create a string by assigning text to a variable:
name = "John"
city = "Delhi"
course = "Python Programming"You can also print strings directly:
print("Hello, World!")Strings Can Contain Numbers and Symbols
A string doesn't have to contain only letters.
phone = "9876543210"
price = "$99.99"
code = "A123"Even though some of these look like numbers, they're strings because they're written inside quotes.
For example:
age = "20"Here age is a string, not an integer.
Multiline Strings
If you need to store text across multiple lines, you can use triple quotes:
message = """Hello John,
Welcome to Python!
Let's start learning."""
print(message)Output:
Hello John,
Welcome to Python!
Let's start learning.Triple-quoted strings can also be useful for documentation and docstrings.
Accessing Characters
A string is a sequence of characters, and each character has an index.
Python starts indexing from 0.
word = "Python"Its indexes look like this:
P y t h o n
0 1 2 3 4 5So we can access individual characters:
word = "Python"
print(word[0])
print(word[2])Output:
P
tNegative Indexing
Python also supports negative indexes.
P y t h o n
-6 -5 -4 -3 -2 -1For example:
word = "Python"
print(word[-1])Output:
nString Slicing
Slicing lets us extract part of a string.
word = "Python"
print(word[0:3])Output:
PytThe syntax is:
string[start:end]The end index is not included.
You can also leave out the start or end:
word = "Python"
print(word[:3])
print(word[3:])Output:
Pyt
honJoining Strings
You can combine strings using the + operator.
first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name)Output:
John SmithRepeating Strings
The * operator can repeat a string.
print("Hi! " * 3)Output:
Hi! Hi! Hi!Finding the Length
The len() function tells you how many characters a string contains.
name = "John"
print(len(name))Output:
4Spaces are also counted as characters.
text = "Hello World"
print(len(text))Strings are Immutable
One important thing to know is that strings cannot be changed directly after they are created.
For example, this won't work:
word = "Python"
word[0] = "J"Python will raise an error.
Instead, you can create a new string:
word = "Python"
word = "J" + word[1:]
print(word)Output:
JythonUseful String Operations
Python provides many methods for working with strings.
For example:
text = "hello python"
print(text.upper())
print(text.lower())
print(text.title())Output:
HELLO PYTHON
hello python
Hello PythonWe'll cover these and other string methods in detail in a separate topic.
Checking Text Inside a String
You can use in to check whether some text exists inside another string.
message = "I am learning Python"
print("Python" in message)Output:
TrueYou can also use not in:
print("Java" not in message)Output:
TrueEscape Characters
Sometimes you need to include special characters inside a string.
For example, to include quotation marks:
message = "He said, \"Hello!\""
print(message)Output:
He said, "Hello!"The backslash (\) is called an escape character.
Some commonly used escape sequences are:
Escape Sequence | Meaning |
|---|---|
| New line |
| Tab |
| Double quote |
| Single quote |
| Backslash |
Example:
print("Hello\nJohn")Output:
Hello
JohnUsing f-Strings
f-strings make it easy to include variables inside strings.
name = "John"
age = 20
print(f"My name is {name} and I am {age} years old.")Output:
My name is John and I am 20 years old.You'll use f-strings frequently when creating formatted output.
A Practical Example
Let's create a simple welcome message:
name = input("Enter your name: ")
language = "Python"
print(f"Hello {name}!")
print(f"Welcome to {language} programming.")If the user enters John:
Hello John!
Welcome to Python programming.Strings may seem simple at first, but you'll use them everywhere in Python—from displaying messages to processing user input and handling data.