Python String: Creating a String
Learn the basic ways to create strings in Python. This page focuses only on making string values, not changing or processing them.
name = "Alice"
message = 'Hello'
empty = ""
text = str(123)
print(name)
print(message)
print(empty)
print(text)
Note: Use single quotes, double quotes, or str() to create a string. Triple quotes are useful for multi-line text.
What this page covers #
- What a string is in Python
- How to create a string with quotes
- How to create an empty string
- How to convert other values to strings with
str() - When to use single, double, or triple quotes
What a string is #
A string is text data in Python.
Strings can contain:
- Letters
- Numbers
- Spaces
- Symbols
Examples of strings:
"hello"
"123"
"Python is fun"
Even though "123" looks like a number, it is a string because it is inside quotes.
You can check the type with type():
text = "123"
number = 123
print(type(text))
print(type(number))
Output:
<class 'str'>
<class 'int'>
If you are new to strings, see Python strings explained: basics and examples.
Create a string with quotes #
The most common way to create a string is to put text inside quotes.
You can use single quotes:
greeting = 'hello'
print(greeting)
Or double quotes:
greeting = "hello"
print(greeting)
Both create a string. In most cases, there is no difference.
Choose the style that makes your text easier to write.
Create an empty string #
An empty string is a string with no characters in it.
Use either "" or '':
empty1 = ""
empty2 = ''
print(empty1)
print(empty2)
print(type(empty1))
Output:
<class 'str'>
An empty string is often used as a starting value before adding text later.
Create a multi-line string #
Use triple quotes when you want text to span more than one line.
You can use triple double quotes:
message = """Hello
Welcome to Python
Have a nice day"""
print(message)
Or triple single quotes:
message = '''Line 1
Line 2
Line 3'''
print(message)
Python keeps the line breaks inside the string.
Use triple quotes when you really need multiple lines. For normal one-line text, single or double quotes are usually clearer.
Convert values to strings with str() #
The str() function converts other values into strings.
Example with a number:
text = str(42)
print(text)
print(type(text))
Output:
42
<class 'str'>
You can also convert other values:
print(str(True))
print(str(3.14))
This is useful when you need to combine text with other data.
age = 25
message = "Age: " + str(age)
print(message)
If you want to learn this function in more detail, see Python str() function explained or how to convert int to string in Python.
Single quotes vs double quotes #
There is no major difference between single quotes and double quotes for normal strings.
Both of these are valid:
a = 'Python'
b = "Python"
A common rule is:
- Use double quotes if the text contains a single quote
- Use single quotes if the text contains double quotes
- Pick one style and stay consistent
Example:
text1 = "It's working"
text2 = 'She said "hello"'
print(text1)
print(text2)
This helps you avoid unnecessary escaping.
Escaping quote characters #
A quote character can end a string too early if it matches the quote used to start the string.
For example, this causes a problem:
# This will cause an error
# text = 'It's working'
Python sees the second ' in It's as the end of the string.
To fix this, use a backslash:
text = 'It\'s working'
print(text)
You can also switch quote styles:
text = "It's working"
print(text)
In many cases, switching quote styles is simpler than escaping.
If you get a quote-related error, see SyntaxError: EOL while scanning string literal.
Common mistakes #
Here are some common problems beginners run into when creating strings.
Forgetting quotes around text #
If you write text without quotes, Python treats it like a variable name.
# This will cause an error
# name = Alice
Python may raise a NameError because Alice is not defined.
Correct version:
name = "Alice"
print(name)
Mixing quote characters incorrectly #
This can break the string early and cause a syntax error.
# This will cause an error
# text = 'It's good'
Fix it by escaping the quote or changing quote styles:
text1 = 'It\'s good'
text2 = "It's good"
print(text1)
print(text2)
Using triple quotes when a single-line string is enough #
Triple quotes work, but they are usually best for multi-line text.
text = """hello"""
print(text)
This is valid, but for one line, this is simpler:
text = "hello"
print(text)
Assuming 123 and "123" are the same type #
They are different:
value1 = 123
value2 = "123"
print(type(value1))
print(type(value2))
Output:
<class 'int'>
<class 'str'>
Trying to join text and numbers without converting first #
This causes a type error:
# This will cause an error
# age = 10
# message = "Age: " + age
Fix it with str():
age = 10
message = "Age: " + str(age)
print(message)
For this specific error, see TypeError: can only concatenate str not int to str.
Useful debugging checks #
If you are not sure what value you created, these commands help:
text = "hello"
print(text)
print(type(text))
print(repr(text))
print(text)shows the valueprint(type(text))shows the data typeprint(repr(text))shows the exact string representation, which is helpful for spaces and escape characters
FAQ #
How do I create a string in Python? #
Put text inside single quotes or double quotes, like 'hello' or "hello".
What is the difference between single and double quotes in Python? #
Usually none. They both create strings. Choose the one that avoids extra escaping.
How do I create an empty string? #
Use "" or ''.
How do I turn a number into a string? #
Use str(), like str(10), which returns "10".
How do I write a string on multiple lines? #
Use triple quotes so Python keeps the line breaks.