Double vs Single Quotes

A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers.

String literals in python can be declared with either single or double quotes, but make sure you stick to the same convention through the same film.


my_other_str = 'A hat'
my_str = "a cat" 

Above both will be fine. Below is one when you type on terminal

>name = "Colt"
>name2 = 'Colt'

But when you use print statement output will exactly same like you assigned


If you are using double quote inside single or single code inside double code it will be work perfectly

>Msg="Dad said 'don't waste food'"
Dad said 'don't waste food'

String Escape characters

What's String Escape Characters?

An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

In python there are also "escape characters", which are "meta characters"- they get interpreted by python to do something special:


#//When we need new line between word and sentences 
>New_line = "hi\nthere"
hi                 (output will be)
there           (output will be)

#//When you wanted to add backslash
>Str="this is a backslash: \\"
this is a backslash: \

#//When you want double code inside double code while assigning string to variable
>str = "he said \ "ha ha \""
he said "ha ha"

String Concatenation

Concatenation is combining multiple string together. In python you can do this simply with the "+" operator.

>str_one = "my"
>str_two = "love"
>str_three = str_one + " "+ str_two 
My love

#//When we concatenation string with string
>username = "karthi"
>print("helo there and welcome to the exam hall" + username)
helo there and welcome to the exam hall, karthi

#//When we concatenation integer with string
>8 + "hello"
Traceback (most recent call lates):
File "<stdin>", line 1, in <module>
typeERROR: unsupported operand type(s) for +: 'int' and 'str'

Tips: you can also use the "+=" operator to reassign the value for example

>Str_one = "ice"
>Str_one += "cream"
Ice cream

String Formatting

There are also several ways to format string in Python to interpolate variables. The new way (new in python 3.6+) => F-string


>Formatted = f"I've told you {x} times already!"
I've told you 10 times already

>Guess = 8
>Print (f "your guess of {guess} was incorrect!"
>Your guess of 8 was incorrect!
>Print (f"your guess of {guess} was incorrect!")

F string format won't work below python 3.6 version instead of F string format we have to use dot format. Please check below example for dot format

>Ripness = "unripe"
>Print("The {} is {}" .formate(fruit, Ripeness))

Indexes in string

Each string has index, we can even access string individually by letters. The Index in python starts with 0 string-index-in-python

>"yes"[0]  (when code like this we can individually access the string like this)

>name = "karthi"

#// access index which does not present
traceback (most recent call last):
file "<stdin>", line 1, in <module>
indexerror: string index out of range    (we will get this when the index is overflow)

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to for any queries

If you already registered then please do check your email for the Webex link
Join My Facebook Group