Python string type everything you need to know

Python also provides us with a way to utilize a text or a sentence in our programs using the data type known as string. Using this data type we can manipulate any text in our program. Only string literals can be assigned to string data type object. A string literals are a sequence of characters, they can also contain escape sequence and a prefix.

More about string literals here: Python string literals

Note Python string type are a sequence of Unicode code point. Since it can Unicode characters using string type we can use any of the known languages scripts in the world, may it be Japanese, Hindi, Russian, etc.

The syntax to write a string in our programs is- an object name followed by an equal to(=) sign, the text is written under a single quotation or a double quotation or a triple quotes(single or double quotation written three times).An example is given below.

>>> Str='Candcplusplus is the best' #using single quotation
>>> Str
'Candcplusplus is the best'
>>> str="Candcplusplus is still the best" #double quotation
>>> str
'Candcplusplus is still the best'
>>> #Using the triple single quotation
>>> Str1='''candcplusplus is great!!!''' 
>>> Str1
'candcplusplus is great!!!'
Advantage of triple single quotes or triple double quotes over the single quotes and double quotes

There is an advantage of triple single or triple double quotation over the single and double quotation. With triple single quotation we can continue the text in the new line. Look at the code example.

st='''C makes it easy to shoot yourself in the foot; #continue to next line
C++ makes it harder,but when you do it blows your whole leg off.'''
>>> st
'C makes it easy to shoot yourself in the foot;\nC++ makes it harder,but when you do it blows your whole leg off.'
>>> str2="""New
Year"""
>>> str2
'New\nYear' 
>>> stt='First line #Trying to write in the next line
SyntaxError: EOL while scanning string literal #Error!!
>>> st1="First line
SyntaxError: EOL while scanning string literal #Error!!

With the single or the double quotes it is impossible to continue the sentence in the next line.

If you are wondering what the character ‘\n‘ (known as newline escape sequence) stands for, it signify that the sentence or the text following it will be written in the next line.Consider the code below.

More about esacep sequence here : Python escape sequence

>>> st3='Merry Christmas \nhappy New Year!!' #Note the '\n' before 'happy'
>>> print(st3)
Merry Christmas 
happy New Year!!
>>> St4="Fist \nSecond \nThird"
>>> print(St4)
Fist 
Second 
Third

With print() we printed out the string in the IDLE and you can see for the st3 object the text ‘happy New Year!!’ comes after ‘\n’, so it is written in the next line. Similarly, in St4 ‘Second’ and ‘Third’ is written after the ‘\n’ escape sequence, so they are written in the next line.

More string examples

If you want to write a single or double quotation inside the text use ‘\'(slash).

>>> sen1='''John said:\"Will you marry me\?"
Emma replied:\'No!!\' '''
>>> print(sen1)
John said:"Will you marry me?"
Emma replied:'No!!'

We can perform concatenation of two strings using ‘+’ or ‘*’ sign.

>>> S1="Cand"
>>> S2="plusplus"
>>> S1+S2
'Candplusplus'
>>> 'Candcplusplus ' + 'is the greatest'
'Candcplusplus is the greatest'
>>> 3*'C' #Multiply 'C' three times
'CCC'
>>> "C" + "and" + "C" + 2*"Plus" + ".com"
'CandCPlusPlus.com'

In a raw string, the escape sequence are not treated as a special character, they are treated like normal characters. We can obtain raw string by putting the prefix ‘r‘.

>>> st1="a tab \t end" #a normal string
>>> print(st1) 
"a tab    end" #a raw string
>>> #the '\t' is converted to tab
>>> #A raw string example
>>> st2=R"a tab \t end"
>>> print(st2)
a tab \t end

In the raw string st2 the escape sequence ‘\t’ is not interpreted to tab but treated normally.

Link :Python raw string

As said earlier Python string type support Unicode characters out of the box, so let’s print some Japanese,Russian and Hindi Devanāgarī characters.

>>> Russian="русский\n"
>>> Japanese ="盾の勇者の成り\n"
>>> Hindi="हिन्दी"
>>> print(Russian ,Japanese , Hindi)
русский
 盾の勇者の成り
 हिन्दी

To know about string slicing visit the link: String slicing


Subscribe here:




Leave a Reply

Your email address will not be published. Required fields are marked *