How to create a string in Python?

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

What are strings in Python?

Strings are amongst the most popular data types in Python. A string can be defined as a sequence of characters.

Strings can be created by enclosing characters inside a single quote, double-quotes and triple quotes.

Code

# Single Quotes
my_string = 'Pylenin'
print(my_string)

# Double Quotes
my_string = "Pylenin"
print(my_string)

# Triple Quotes
# can be extended over multiple lines
my_string = """Check out Pylenin
               for Python related blogs and videos"""
print(my_string)

Output

Pylenin
Pylenin
Check out Pylenin
               for Python related blogs and videos

As you can see in the code above, we are assigning a string to a variable. It is done with the variable name followed by an equal sign and the string.

Unless necessary, try not to use triple quotes for strings. It is generally used for writing docstrings in your Python code, which acts like a documentation.

  1. How to access characters in a Python string?
  2. How to replace characters in a string in Python?
  3. How to concatenate strings in Python?
  4. How to iterate through a string in Python?
  5. Check if a Substring is Present in a Given String in Python
  6. Escape sequences in Python String
  7. Python String Formatting - The Definitive Guide
  8. Commonly used Python string methods