Skip to content

Python data type: Strings

Explore Python interpreter

Start python interpreter

➜  ~ python
Python 3.8.7 (default, Mar  4 2021, 21:47:43)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Show how to exit the python interpreter * exit() * Ctrl + D

Mention: * auto-completion with Tab * history with up/down arrows

Show comment:

>>> # this is my comment
>>>

Starting with strings

Create a variable:

>>> hostname = 'router1'
>>>
>>> hostname
'router1'

Explain that you have to use the same quote types:

>>> 'router1"
  File "<stdin>", line 1
    ' "
       ^
SyntaxError: EOL while scanning string literal
>>>

Explain that spaces are not important:

>>> hostname='router1'
>>> hostname=        'router1'
>>> hostname        =        'router1'
>>> hostname = 'router1'
>>> 

Explain type()

>>> type('router1')
<class 'str'>
>>> type(hostname)
<class 'str'>
>>>

Difference between variable and print():

>>> hostname
'router1'
>>> print(hostname)
router1

>>> ip_address = 'The IP address is:\n\n10.1.1.1'
>>>
>>>
>>>
>>> ip_address
'The IP address is:\n\n10.1.1.1'
>>>
>>>
>>> print(ip_address)
The IP address is:

10.1.1.1
>>>