Skip to main content

Python Single vs Double Quotes Gorakh Gupta The Crazy Programmer

We know that if we want to print something on the console then we use print() statement in python. In a print statement, we can use single quotes or double quotes to print the text to the console.

In this article, we will see what is the difference between single and double quotes in Python and in which scenario we have to use one.

In languages like Java, C++, and C when we want to print something we strictly have to use double quotes. This thing is different in the case of Python Language. In Python, we can use either single quotes or double quotes.

Difference between Single and Double Quotes in Python

In the case of Python, there is no major difference between the quotes. We can use either of them in the print statement.

Now, we will see some scenarios that why both the quotes exist in Python while not present in other Languages.

Let’s suppose we want to print a statement like This is Hari’s Book.

By using double quotes we can easily print this statement.

Code:

print("This is Hari's Book.")

Output:

This is Hari's Book.

But when we try to print the same statement using single quotes then we will get an error.

Code:

print('This is Hari's Book.')

Output:

File "c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py " , line 1
     print('This is Hari's Book.')
                                   ^
 SyntaxError : invalid syntax.

Here it is showing an invalid syntax. Let’s see why this occurred.

As we can see in the code we have used single quotes just before This and we also used single quotes after Hari and after Book.

But we know that if we have opened a single quote then we have to close it also, and the interpreter assumes the single quote after Hari as their closing single quote. That is why it is giving errors to us.

Now let’s see the solutions for the above error.

Solution 1: By Using Escape Sequence Characters

We can use a special character called Escape Sequence in our statement when we want to use any of the quotes in the statement.

Code:

print('This is Hari\'s Book.')

Output:

This is Hari's Book.

Now we can easily print the required statement without any error.

The escape sequence tells the interpreter to escape the next character, so the single quote after Hari is not concluded as a closing quote.

Solution 2: By Using Opposite Quotes

If we want single quotes in our statement then we have to use double quotes in the print function and vice-versa. That means if you want a double quote in your printed statement then you have to use single quotes in the print function.

For the above statement as we can see we want a single quote in our statement then we have to use a double quote in the print statement.

Code:

print("This is Hari's Book.")

Output:

This is Hari's Book.

If we want to see a double quote in our statement then we have to use single quotes in the print function.

Code:

print('This is Hari"s Book.')

Output:

This is Hari”s Book.

Conclusion

We can use a single quote or double quote in the print function to print a statement in python. But if we want a single quote in our statement, then we strictly have to use double quotes in the print function. To perform this action, we can also use escape sequence characters.

If we want a double quote in the statement, then we strictly have to use a single quote in the print function.

The post Python Single vs Double Quotes appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/r8a5eSJ

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20