Python Rules Of Coding: String literals

avatar
(Edited)

Previous
Python Rules Of Coding: Comments

code-1084923_1280.png

Source

Python Rules Of Coding: String literals

print(" Hello, World! ")

In the above code, the string literal is "Hello, World!" with quotation marks and the string value is Hello, World! without quotation marks. In short, the string value is the output from a string literal code. Therefore we understand that string literals and string values are not the same.

Sometimes we need quoting a source code in string values. For this why we have to add additional formatting to string literals so that we get our expected string values as output.

Quotes

We can use double quotes to enclose a string literals if we want to embed single quotes within a string value

"He says, 'Hello dear!' "

Output the string value:

He says, 'Hello dear!'

Same way, use single quotes to enclose the string literals and embed double quotes within the string value.

'He says, "Hello dear!" '

Output the string value:

He says, "Hello dear!"

Multiple Lines

Sometimes it needs to print strings on multiple lines with grouped, orderly and formatted as a poem, lyrics, letter.

In this case, we can use triple double quotes (" " ") or triple single quotes (' ' ')

" " "
Multiple lines
string literals.
Enclosed by triple double-quotes. 
" " "

'''
Multiple lines
string literals.
Enclosed by triple single-quotes. 
'''

Escape Characters

To format the string literals in a specific way we can use
Escape character. Along with another character, Escape character begins with the backslash ( \ ) .

Here is a list of some escape characters from w3schools:

escape1.JPG

 str = 'It\'s OK.'
  print(str)

Output:

It's OK.

str = "Embed backslash \\ in string value."
 print(str) 

Output:

Embed backslash \ in string value.

We can also remove whitespace by using the backslash ( \ ) escape character. Learn more

There is another kind of string value called Raw strings. Formatted string literals can be outputted as a string value by using an escape character ' r ' in front of a string literals.

print(r"Rony says,\"The balloon\'s color is black.\"")

Output

Rony says,"The balloon's color is black."

Next
Python Rules Of Coding: User Documentations

----------------------------------------------------------------------



0
0
0.000
13 comments
avatar

Congratulations @alrashel! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You received more than 1250 upvotes. Your next target is to reach 1500 upvotes.

You can view your badges on your board and compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

Revolution! Revolution!
Vote for us as a witness to get one more badge and upvotes from us with more power!
0
0
0.000
avatar

This is a nice approach to python rule of coding. We like contents like this (tutorials)on our platform, and we would be glad to see you around. Join our community and check the gitplait website. Thank you

0
0
0.000
avatar

I use the \n escape character a lot when it comes to labels with Python's matplotlib and seaborn data visualization plots.

0
0
0.000