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:
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.
Great! Proceed to the next page ...