Introduction To Python - W2 - C8
Introduction To Python - W2 - C8
Tharaka Ilayperuma
Content
The print function can take a number of arguments - each has a default value
print(“Hello”) This will result in applying the default values for the arguments above
• New lin
print() function – examples (1)
The end keyword argument
Use file keyword argument to indicate a file that was open in write or append mode to
write messages to it
This statement is creating/opening the file called “output.txt”
and creates alias “f” to access the output.txt file
Formatting your output (1)
Different formatting options are available to display the results as per the user
requirements
• formatted string literals (f-string) https://docs.python.org/3/tutorial/inputoutput.html
• str.format() method
Read more on
formatting options
Using formatted string literal or f-string to format your output
here
e.g. year = 2023
course = ‘Introduction to Python’
print(f'Results of the course: {course} in {year}’)
Output:
Results of the course: Introduction to Python in 2023
let you include the value of Python expressions inside a string by prefixing
the string with f or F and writing expressions as {expression}
Formatting your output (2)
Using str.format() to format your output • When placeholders { } are empty, Python will
replace the values passed through
str.format() in order
• each individual value contained in the format
method can be called by its index number,
which starts with the index number 0
Formatting your output (3)
Type setting and padding
Type Meaning
If you need to convert the user input to any other type, you need to do it explicitly
• num = int(input("Enter a number: "))
name = input ("Enter your name: ") >>> Enter your name: Philip Windridge
email = "[email protected]"
split() method breaks the given input by the specified separator. If a separator is not
provided then any white space is a separator
Write a Python program to read the First name, Last name, and salary of an employee of
an employee and display the output in the following manner.
S2 S1 S2 true
S1
S3
End
End End