Formatted Output CSE 1310 – Introduction to Computers and Programming 1.

Slides:



Advertisements
Similar presentations
Months of the year December January November October February
Advertisements

Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chubaka Producciones Presenta :.
The Months and The Seasons Prepared by Claudia Doria and Terra Myers.
2012 CALENDAR. JANUARY 2012 Sunday 日 Monday 月 Tuesday 火 Wednesday 水 Thursday 木 Friday 金 Saturday 土
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
January 2012 Monday Tuesday Wednesday Thursday Friday Sat/ Sun / /8 14/15 21/22 28/
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chicas, este calendario si es pa' nosotras !!!!!.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAY WEEK WEEK WEEK WEEK WEEK CALENDAR PROJECT.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.

January 2013 MondayTuesdayWednesdayThursdayFridaySaturdaySunday.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
What day is today? What`s the date?. Sunday Monday Tuesday Wednesday Thursday Friday Saturday What day is today?
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Formatted Output CSE 1310 – Introduction to Computers and Programming 1.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Months of the year. jANUARY New year’s day fEBRUARY Valentine’s day.
Calendar for 2011 Months of the Year with Holidays.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
TEMPORAL VISUALIZATION OF DATA FROM THE FRENCH SENTINEL NETWORK.
The Calendar.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
TMF1414 Introduction to Programming
FY16 Budget Build (Banner)
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
TIMELINES PHOTOS This is an example text
TIMELINES PHOTOS This is an example text
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
McDonald’s Kalender 2009.
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Problem Gambling Clicks to Opgr.org
2300 (11PM) September 21 Blue line is meridian..
McDonald’s calendar 2007.
1 - January - Sun Mon The Wed Thu Fri Sat
Proud As A Peacock! We are very proud of__________________
Teacher name August phone: Enter text here.
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
February 2007 Note: Source:.
MONTHS OF THE YEAR January February April March June May July August
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
McDonald’s calendar 2007.
Production Month Sun Hours K Monthly Kwh Tou Peak Value After Kwh
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

Formatted Output CSE 1310 – Introduction to Computers and Programming 1

Importance of Formatting When we print information on the screen, formatting can make a big difference: Nicely formatted printouts are much easier for a user to read. For example: in printing a matrix: [[ , , ], [9.12, , 1], [32.34, 1.232, 0.2]] 2

Importance of Formatting When we print information on the screen, formatting can make a big difference: Nicely formatted printouts are much easier for a user to read. For example: in printing a matrix:

The format Method for Strings my_string.format(item1, item2, …, item_n) Looks for placeholders, marked by {} in my_string, and replaces them with the specified items. Simple example: my_string = "{} is today, and {} is tomorrow" print(my_string.format("Tuesday", "Wednesday")) OUTPUT: 'Tuesday is today, and Wednesday is tomorrow' 4

5 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] print(matrix1) OUTPUT: [[ , , ], [9.12, , 1], [32.34, 1.232, 0.2]] Example 1: no formatting here, just printing the matrix as a list.

6 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print(item) OUTPUT: [ , , ] [9.12, , 1] [32.34, 1.232, 0.2] Example 2: again no formatting, just printing each line separately.

7 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print("{:> 11} {:> 11} {:> 11}".format(item[0], item[1], item[2])) OUTPUT: Example 3: Here we use formatting: – :> means "right alignment" – 11 means "minimum width of 11 for that item"

8 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print("{:> 5} {:> 5} {:> 5}".format(item[0], item[1], item[2])) OUTPUT: Example 4: Here we change the previous example, by setting the minimum width to 5 instead of 11: – The columns are not aligned anymore. – Why?

9 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print("{:> 5} {:> 5} {:> 5}".format(item[0], item[1], item[2])) OUTPUT: Example 4: Here we change the previous example, by setting the minimum width to 5 instead of 11: – The columns are not aligned anymore. – Why? Because some items need more than the minimum width.

10 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print("{:> 5} {:> 5} {:> 5}".format(item[0], item[1], item[2])) OUTPUT: Example 4: What is a good value for the minimum width?

11 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print("{:> 5} {:> 5} {:> 5}".format(item[0], item[1], item[2])) OUTPUT: Example 4: What is a good value for the minimum width? It should be large enough to accommodate the longest item.

12 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print("{:> 11f} {:> 11f} {:> 11f}".format(item[0], item[1], item[2])) OUTPUT: Example 5: we use {:> 11f}. f specifies that the item should be printed as a float. In floats, by default six decimal places are printed.

13 matrix1 = [[ ,.21312, ], [9.12, , 1], [32.34, 1.232, 0.2]] for item in matrix1: print("{:> 8.3f} {:> 8.3f} {:> 8.3f}".format(item[0], item[1], item[2])) OUTPUT: Example 6: {:> 8.3f}. 8.3f means: print a float, with a minimum width of 8, and 3 decimal places printed for each float.

Specifying format While there can be more complicated versions (see textbook Section 4.7), the most common format specifications are these: {:[align] [minimum_width] [.precision] [descriptor]} Square brackets indicate optional parts (that may be skipped). 14

Specifying format While there can be more complicated versions (see textbook Section 4.7), the most common format specifications are these: {:[align] [minimum_width] [.precision] [descriptor]} The alignment can be of three types: – < for left alignment – > for right alignment – ^ for center alignment 15

Specifying format While there can be more complicated versions (see textbook Section 4.7), the most common format specifications are these: {:[align] [minimum_width] [.precision] [descriptor]} The most common descriptors are: – s for string – d for integer – f for floating-point number – e for floating point number in scientific (exponential) notation – % for floating point number as percent. 16

Example: Months and Days month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] for i in range(0, 12): print("{:>13s}: {:>2} days".format(month_names[i], month_lengths[i])) 17

Example: Months and Days: Output January: 31 days February: 28 days March: 31 days April: 30 days May: 31 days June: 30 days July: 31 days August: 31 days September: 30 days October: 31 days November: 30 days December: 31 days 18