3. Text in Python
Data does not have to be numbers. Text is also a useful data type, which is called string Unlike integer and float data types, strings are enclosed in single or double quotation marks (' ' or " ").
1
"Hello"
Copied!
You can perform operation on strings:
1
"Hello" + " mary"
2
>> Hello mary
3
4
string1 = "Hello"
5
string2 = " Eric"
6
string1 + string2
7
>> Hello Eric
Copied!
Note that blank space matters in strings. e.g."hello" and " hello"are two different string values.
You can find out the length of a string (e.g. how many letters) as follow:
1
len('hello')
2
>> 5
3
4
string2 = " Eric"
5
len('Hello'+string2)
6
>> 10
Copied!
Note that len() is a function. A function does some useful work on the an input (content inside the bracket; in this case, the string "hello") and outputs something (e.g. an integer showing the length of the string)
Last modified 1yr ago
Copy link