Monday, 16 May 2016

Learn python the easy way - 3. Strings

String is nothing but an text. The plain text is called as string in programming. It can be done by adding two single or double quotation marks between the text.

>>>"Python is easy!"
'Python is easy'

In string, some characters are cannot be included directly. Because each character in Python has represent something. In order to print that characters as a string, we use escaping character (back slash) \.

for an example

>>>"Hello "How are you"?"
  File "<stdin>", line 1
    Hello "How are you"?"
                      ^
SyntaxError: invalid syntax

To avoid this, add back slash before the character

>>>"Hello, \"How are you\"?"
'Hello,  "How are you"?

New Lines

Add \n to get a new line.

>>>print("Hello \nPython!")
Hello
Python!


No comments:

Post a Comment