Introduction to Python
  • 1. Introduction
  • 2. Numbers in Python
  • 3. Text in Python
  • 4. Making Comparisons
  • 5. Making Choices
  • 6. Exercise: Paper, Scissors, Stone game
  • 7. Lists
  • 8. Loops
  • 9. Simple Web Crawler
Powered by GitBook
On this page

Was this helpful?

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 " ").

"Hello"

You can perform operation on strings:

"Hello" + " mary"
>> Hello mary

string1 = "Hello"
string2 = " Eric"
string1 + string2
>> Hello Eric

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:

len('hello')
>> 5

string2 = " Eric"
len('Hello'+string2)
>> 10

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)

Previous2. Numbers in PythonNext4. Making Comparisons

Last updated 5 years ago

Was this helpful?