Strings, Casting, and User Input

In Python, text is represented as a string, which is a sequence of characters: letters, digits, and symbols. You indicate that a value is a string by…

Strings
In Python, text is represented as a string, which is a sequence of characters: letters, digits, and symbols. You indicate that a value is a string by putting either single or double quotes around it.
Whenever you create a string by surrounding text with quotation marks, it is called a string literal, because the string is literally written out in your code.
Joining strings
The + operator concatenates two strings, joining them end to end with nothing in between. If you want a space, you have to supply one yourself.
Casting
The int(), float() and str() functions can be called to cast a value to an integer, a float, or a string. Casting a float to an int truncates it: the decimal part is dropped, never rounded.
CallResult
int("3")3, an integer
float("3")3.0, a float
int(1.8)1, the decimal part is dropped
str(1.8)the text 1.8
Program input and output
Program input is data sent to a computer for processing by a program. Input can come in a variety of forms:
• tactile, such as a swipe on a tablet
• audio, such as a voice to be processed
• visual, such as an image to be filtered
• text, typed at the keyboard or read from a file
Program output is any data sent from a program to a device, and it comes in the same variety of forms.
Reading input with input()
The input() function obtains information from the user. The program waits for the user to type something, and the value can be stored in a variable once the user presses Enter. Since user input almost always needs an explanation, input() optionally accepts a string that it prints just before the program stops to wait.
The type() function lets you see the datatype of a value.
Turning input into a number
Because input() hands you text, you have to cast it before doing arithmetic with it.
Functions, and composing them
print(), int(), float(), str(), type() and input() are functions, no different from the functions in your math class. If f(x) = x squared, then f(3) is 9. In the same way int(4.5) is 4, and float("3") is 3.0.
Functions compose too. If g(x) = 3x, then f(g(2)) is 36: the inner function runs first and its result becomes the input to the outer one. Python works identically, so int(float("3.2")) is 3.