Skip to main content
  1. Posts/

Python Variable and String

·247 words·2 mins
Table of Contents

Today I was using python and I learned how to use Variables and Strings.

Variable #

So what is a variable? A variable is used to store information or a value for example

lives = 3

which also reads out as 3 is assigned to lives variable. The left side to the equal sign (=) is called the identifier and to the right it is called the value.

NOTE: Do not use keywords as the variable names as those keywords are reserved by Python.

For example, we cannot name a variable print, if, as or class.

What happened if we still decide to use reserved keywords as variable names? The following is an example of not to do:

print = "names"
print(print)

it will show up a error message instead of names like this

>>> print = "names"
>>> print(print)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> 

What is a string? #

A string is a line of characters inclosed in quotation marks. For example:

  • 'Hello world'
  • "This is a string in Python"
  • '3'

We can use either single, double or even tripple quotes, however the open and closing quotes have to match or the same type.

With string python, we can use triple quotes for multi-line strings.

>>> multiline_string = '''
... This is the first line
... second line
... and third line.'''
>>> print(multiline_string)

This is the first line
second line
and third line.
>>>