FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 4
PAGE 3 / 4
Data Types
Home >>> Free Online Lessons, Python 3

Strings

We have already used strings. To declare a variable that holds a string, we simply write the text between double quotes or single quotes. Explicit conversion to this type is done using the str() function.

Example: Look at the instructions below:
Editor - lesson_4.py
       
Console / Output done
We can introduce character strings that span multiple lines. In this case, we use triple quotes or triple apostrophes:

Examples. Test the code below in the editor:

s4 = '''A text written
on two lines.'''
s5 = """Another text written
on two lines."""
print(s4)
print(s5)

Numeric Data vs. Strings

Python is intuitive when assigning values to a variable, as shown below:

v1 = 20 # the type is int
v2 = 10.3 # the type is float
v3 = "magic" # the type is str
print(v1, v2, v3)


Of course, if we try to add v1 with v2, the result will be 30.3, which is of type float. However, if we try to add v1 with v3, we will get an error - we cannot add an integer with a string...
Test all examples and read the text.
 home   list  CONTENTS   perm_identity   arrow_upward