Basic IO and string formatting

Basic IO - User input

the raw_input() function - Python2 way

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'>
		

the input() function - Python3 way

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!

Examples (python3)


			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

Formatting strings

Why and How?

It is not convenient to use the concatenation operator when we want to display some message and some variables interpolated into it.
Python do not support variables interpolation like in bash/Perl/PHP/...

				# not possible in Python!!!
				$name = "Iva";
				print("Hello $name !")
					Hello Iva !
			

"old-style" String Formatting in Python

syntax


			"format_string" % (values_tuple)
		

			>>> "%s died in %d year" % ("Ada Byron", 1852)
			'Ada Byron died in 1852 year'
		
format_string - a string with formatting placeholders in it
values_tuple - the values which will be interpolated into the respective formatting placeholders.

how it works?

Example - format data as table


			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 |
			| ========== | ========== |
		

Examples - more


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

it's the "old style of formatting" - should we use it?

Though considered "the old way" it's still widely used and is not (still) deprecated in new Python3 versions
But the preferred way of formatting strings in Python is str.format() method
Knowing how this string formatting works, will help you to grasp the "new style", discussed in next slides.

resources

Formatted Output @python-course.eu
printf-style String Formatting @docs.python.org/3.6

format string syntax: str.format() method

Syntax


			"string_with_placeholders".format(values_to_be_substituted)
		

			>>> "{} died in {} year".format("Ada Byron", 1852)
			'Ada Byron died in 1852 year'
		
string_with_placeholders is a string containing placeholders of the form {}
values_to_be_substituted - a comma separated list of values, which will be replaced in the respective placeholders

placeholders

You can give in placeholders an explicit positional index (counting starts from 0), which allows values to be printed in desired order


				>>> "{1} died in {0} year".format("Ada Byron", 1852)
				'1852 died in Ada Byron year'
			

new style vs old style?

The format specifier syntax is similar to the old %-formatting, with the addition of the {} 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))
		

resources

Python String format() @programiz.com
Format String Syntax @docs.python.org/3.6

examples


				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

Hakimel's reveal.js

framework