What are the Different ways to Create Strings in Python

In this Python tutorial, you will learn “what are the different ways to create strings in Python?”.

A string is a data type in Python that denotes a collection of characters. A string is immutable, meaning once created, you can’t change it. Using the string, you can represent the text.

In my Python project, I mostly use strings to represent the name of any entity, such as a user name, product name, or description.

So, in this tutorial, I have explained how to create a string using different ways, such as enclosing the sequence of characters within the single quote to create a new string.

Additionally, I have explained how to declare a string in Python and what it means.

What are the Different Ways to Create Strings in Python?

First, let me define a string. A string is a sequence of characters in a specific order. It can contain numbers, whitespace, letters, or any symbols.

Here, I will explain different approaches to creating strings, such as using single and double quotes and triple single and double quotes. You will learn how to create detailed strings.

Creating and Storing Strings in Python using the Single and Double Quote

In Python, you can use the single quote (‘ ‘) and double quote (” “) to create strings, but there is a difference in the way they handle the strings.

First, let’s create a string with a single quote. To make a string, wrap the characters or sequence of characters with a single quote, as shown in the code below.

task='Completed the project by 14-May 2024'
print(task)
Creating and Storing Strings in Python using the Single Quote

Look, the string ‘completed the project by 14-May 2024’ is wrapped by a single quote (‘ ‘) and stored in the variable named task; as a result, when the variable task is printed, it returns the string.

READ:  Python Program to Count Upper and Lower Case Characters [3 Ways]

To create a string using the single quote, wrap the sequence of characters within (‘ ‘). But using the single quote you can create only a line of string, if you want to create a multiple of strings, then it is not suitable, and it shows error, when multiple line of string is created using the single quote.

Remember, if you want to create a string using a single quote that contains the single quote, you can’t because the Python interpreter gets confused about the end of the string, which means multiple single quotes lead to this error.

For example, try to wrap this sequence of characters (Kasey going out of town at 9 o’clock) with a single quote to create a string name.

name= 'Kasey going out of town at 9 o'clock'
Creating and Storing Strings in Python using the Single Quote Error

Look, it returns the error, meaning when using the single quote to create a string, the string itself can’t contain the single quote.

The solution error is to use a double quote to create a string. The double quote (” “) works similarly to a single quote, but the string itself can contain the single quote and doesn’t show any error.

For example, create the exact string as in the above example using the double quote in the code below.

name= "Kasey going out of town at 9 o'clock"
print(name)
Creating and Storing Strings in Python using the Double Quote

Look at the output. The string is created using double quotes and contains a single quote.

So, there is not much difference between using single and double quotes while creating a string; the only difference is the personal style or organization for consistency in the database.

This is how to create and store the string using the single and double quotes in Python. However, one problem with double quotes is creating a multiline of strings.

READ:  Indexing and slicing in Python

So what is the solution? There is more than one solution, but one of the solutions is to use triple quotes; look at the next section.

How to Create a String in Python Using the Triple Quotes

In the preceding section, you have used single and double quotes to create a string, but they are limited because they cannot create multi-line strings.

To create a multiline string, you can use triple single quotes (”’ ”’) and triple double quotes (“”” “””).

Just wrap the sequence of characters with triple single or double quotes. For example, look at the code below, which creates a multiline string.

multiline_str = '''Wake Up (7:00 AM): Start the day with a fresh air. Take a moment to stretch and breathe deeply.
Morning Hygiene (7:10 AM): Brush teeth, wash face, and take a shower to refresh and awaken the senses.'''

print(multiline_str)
How to Create a String in Python Using the Triple Single Quotes

Look at the output; a long multiline string is created using the triple single quote (”’ ”’). Similarly, you can use the triple double quotes (“”” “””) to create a string, as shown in the code below.

daily_task="""Work/Study Start (8:30 AM): Begin work or study with the most challenging task.
Tackle it with a fresh mind and undivided attention.
Short Breaks (Every hour): Take a 5-10 minute break every hour. 
Stretch, walk around, or do a quick meditation to reset."""

print(daily_task)
How to Create a String in Python Using the Triple Double Quotes

In the output, you can see the multiline string is created using triple double quotes (“”” “””) and stored in the variable daily_task.

From a functionality perspective, there is no difference between the triple single quotes (”’ ”’) and triple double quotes (“”” “””). Both are used based on preference, consistency and readability.

READ:  How to replace multiple spaces with a single space in Python [3 Methods]

The choice may depend on which makes the code more readable. The above approaches are fundamental ways to create new strings, but apart from this, in Python, there is a function called str(). This function allows you to convert the given values into strings.

So, if you have values like number, float, etc., and want to convert them into strings, pass them to the function str(), as shown in the code below.

zipcode = 90001

str_zipcode = str(zipcode)

print(str_zipcode, "Type is", type(str_zipcode))
Creating a String in Python Using str() Function

Look at the above code; the zip code is declared with the value 9001 of type number, which is the zip code of the familiar cities ‘Los Angeles, Firestone Park, Firestone Pk’. It is passed to the str(zipcode) function to convert this zipcode to a string value.

As a result, the output converts it to a string as “9001”, and its type is <class ‘str’>.

How to Declare String in Python

You can declare the string in Python using the single, double, and triple single or double quotes that you have used in the above sections. Declaring means defining it with a variable name and assigning a string, as shown in the code below.

str_name = " " | ' ' | ''' ''' | """  """"

Where,

  • str_name: It is the name of the variable that will store the strings.
  • ” ” | ‘ ‘ | ”’ ”’ | “”” “”””: This is the single, double, and triple single or double quotes to specify the string.

So, whatever example you have done till now, you created the string by declaring it with different variable names and values.

Now, I hope that you understand how to create strings in Python.

Conclusion

In this Python tutorial, you learned the different ways to create a dictionary using the single quote (‘ ‘) and double quote (” “) or triple single quotes (”’ ”’) and triple double quotes (“”” “””). You also learned how to declare a string.

You may like to read: