❮ Previous Next ❯

Python String

Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use).

String literals can be enclosed by either triple or double or single quotes, although single quotes are more commonly used.

Backslash escapes work the usual way within both single and double quoted literals -- e.g. \n \' \". A double quoted string literal can contain single quotes without any fuss (e.g. "I didn't do it") and likewise single quoted string can contain double quotes. A string literal can span multiple lines, but there must be a backslash \ at the end of each line to escape the newline.

String literals inside triple quotes, """" or ''', can span multiple lines of text.

Example:
x='I am a single quoted string'
y="I am a double quoted string"
z="""I am a tripple quoted string"""
print(x)
print(y)
print(z)
hiii welcome pythonstring1

Here in the above the 1st string enclosed in a single quote, 2nd string enclosed in a double quote and 3rd string enclosed in a triple quote. That means we observed we can print a string in three ways, like using single quote, or using double quote, or using triple quote.

Note:

In a single quoted string , if there is an another single quote then we have to put escape character(that is \) before single quote. Otherwise invalid syntax will come. For example;


pythonstring2

Now it is right. Same case in double quote and triple quote. That means in double quote or triple quote string, if we will put another double quote or triple quote, then we have to put backslash(\) . For example;


pythonstring3
pythonstring4
pythonstring5

❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved