Created for
Allows to prompt the user to enter some value.
variable = raw_input("prompt message, to be displayed to the user: ")
>>> user_input = raw_input("enter some value: ")
enter some value: iva
>>> print user_input
iva
>>> print type(user_input)
<type 'str'>
Allows to prompt the user to enter some value.
variable = raw_input("prompt message, to be displayed to the user: ")
>>> user_input = input("enter some value: ")
enter some value: 42
>>> print(user_input)
42
>>> print(type(user_input))
<class 'str'>
Note, that no matter what value the user entered, it will be stored as a string!
user_name = input("hi, what's your name: ")
user_surname = input("will you tell me your sur name?:")
print("Nice to meet you, ", user_name.capitalize() + " " + user_surname.capitalize() + "!")
please note, that using concatenation operation for strings containing variables values, is not the pythonic way! Next slides will illustrate the proper way to do it
# not possible in Python!!!
$name = "Iva";
print("Hello $name !")
Hello Iva !
"format_string" % (values_tuple)
>>> "%s died in %d year" % ("Ada Byron", 1852)
'Ada Byron died in 1852 year'
print("| %10s | %10s |" % ("=" * 10, "=" * 10))
print("| %10d | %10d |" % (1,2) )
print("| %10d | %10d |" % (34,12) )
print("| %10d | %10d |" % (243,126) )
print("| %10s | %10s |" % ("=" * 10, "=" * 10))
| ========== | ========== |
| 1 | 2 |
| 34 | 12 |
| 243 | 126 |
| ========== | ========== |
x = 1.2345678
y = 123
print("x = %d, y = %d" % (x,y))
print("x = %02d, y = %02d" % (x,y))
print("x = %f, y = %f" % (x, y))
print("x = %.2f, y = %.2f" % (x, y))
str.format()
method
"string_with_placeholders".format(values_to_be_substituted)
>>> "{} died in {} year".format("Ada Byron", 1852)
'Ada Byron died in 1852 year'
{}
>>> "{1} died in {0} year".format("Ada Byron", 1852)
'1852 died in Ada Byron year'
{}
and with :
used instead of %
. For example, '%10d
' can be translated to '{:10d}
' and so on.
print("| {:10s} | {:10s} |".format("=" * 10, "=" * 10))
print("| {:10d} | {:10d} |".format(1, 2))
print("| {:10d} | {:10d} |".format(34, 12))
print("| {:10d} | {:10d} |".format(243, 126))
print("| {:10s} | {:10s} |".format("=" * 10, "=" * 10))
user_name = input("hi, what's your name: ")
user_surname = input("will you tell me your sur name?:")
print("Nice to meet you, {} {} !".
format(user_name.capitalize(), user_surname.capitalize()))
These slides are based on
customised version of
framework