Python Lessons – 1.5 Strings

Python Lessons - 1.5 Strings

Using text

In Python you can use text and you can do it with strings. You can define a string using both the single and the double quotation mark.

>>> 'Hello World!'
Hello world!
>>> "Hello World!"
Hello World!

Often text can contain one of these characters. Use the other character to define this string.

>>> "Hello 'my' World!"
Hello 'my' World!

However, when we want to use special characters enclosed within a string there is the possibility of reporting these characters with a backslash ‘\’.

>>> 'I\'m not so good!'
I'm not so good!

There are also other characters like newline, tab and backslash that can not be expressed with a characte

>>> s =  'I am \n not so good!'
>>> print ( s )
I am
not so good!
>>> s = 'I am \t not so good!'
>>> print ( s )
I am               not so good!
>>> s = ' 6 \ 2 = 3 '
>>> print ( s )
6 2 = 3

There is also the possibility to write a sentence in which you can explicitly address it and this can be done by enclosing the string with “””.

>>>"""I'm happy to be
. . . here"""
"I'm happy to be here"

When you send the lead, the interpreter will wait for the new line with three dots’. . . ‘. The Python console will show this symbol whenever the interpreter will consider a new line necessary to terminate the command.

⇐ Go to Python Lesson 1.4 – Other numeric operations

Go to Python Lesson 1.6 – Input e output ⇒

Leave a Reply