Python also provide us a way to utilize a text or a sentence in our program using data type known as string.Using this data type we can manipulate any text in our program.Note Python string type are sequence of Unicode code point.Since it can Unicode characters using string type we can use any of the known languages script in the world may it be Japanese,Hindi,Russian,etc.
The syntax to write a string in our program in an object name followed by an equal to sign and the text is written under a single quotation or double quotation or 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' >>> Str1='''candcplusplus is great!!!''' #triple single quotation >>> Str1 'candcplusplus is great!!!'
Advantage of triple single or triple double quotation over single and double quotation
There is an advantage of triple single 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' #output >>> 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 Single or double it is impossible to continue the sentence in the next line.
If you are wondering what the character ‘\n’ means,it signify that the sentence or text following it will be written in the next line.Consider the code below.
>>> st3='Merry Christmas \nhappy New Year!!' >>> print(st3) Merry Christmas happy New Year!! >>> St4="Fist \nSecond \nThird" >>> print(St4) Fist Second Third
With print() we print out the string in the IDLE and you can see for ‘st3’ 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 ‘\n’ 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 said:\'No!!\' ''' >>> print(sen1) John said:"Will you marry me?" Emma said:'No!!'
We can perform concatenation of two strings using ‘+’ or ‘*’ sign.
>>> S1="Cand" >>> S2="plusplus" >>> S1+S2 'Candplusplus' >>> 'Candcplusplus' + 'is the greatest' 'Candcplusplusis the greatest' >>> 3*'C' #Multiply 'C' three times 'CCC' #gives three 'C' >>> "C" + "and" + "C" + 2*"Plus" + ".com" 'CandCPlusPlus.com'
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) русский 盾の勇者の成り हिन्दी