0% found this document useful (0 votes)
21 views

Lec5 28 9 2023

This document discusses various ways of formatting output and manipulating strings in Python. It covers using print formatting to control number formatting, using f-strings for formatted output, calculating string length, concatenating strings, repeating strings, and converting between numbers and strings. The key methods and operators discussed include print formatting (% and .format()), f-strings, len(), +, *, str(), int(), and float().

Uploaded by

annie naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lec5 28 9 2023

This document discusses various ways of formatting output and manipulating strings in Python. It covers using print formatting to control number formatting, using f-strings for formatted output, calculating string length, concatenating strings, repeating strings, and converting between numbers and strings. The key methods and operators discussed include print formatting (% and .format()), f-strings, len(), +, *, str(), int(), and float().

Uploaded by

annie naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture 5

1
• >>> q = 459
• >>> p = 0.098
• >>> print(q, p, p * q)
• 459 0.098 44.982
• >>> print(q, p, p * q, sep=",")
459,0.098,44.982
• >>> print(q, p, p * q, sep=" :-) ")
• 459 :-) 0.098 :-) 44.982

2
• >>> print(str(q) + " " + str(p) + " " + str(p *
q))
• 459 0.098 44.982

3
Formatting the Output (Old)
Syntax Output
print('I have %d cats' % I have 6 cats
6)
print('I have %3d cats' I have 6 cats
% 6)
print('I have %03d cats' I have 006 cats
% 6)
print('I have %f cats' % I have 6.000000
6) cats
print('I have %.2f cats' I have 6.00 cats
% 6)

Read more from https://pyformat.info/


Formatting the Output (New)
Syntax Output
print("I have {0:d} cats".format I have 6 cats
(6))
print("I have {0:3d} cats".forma I have 6 cats
t(6))
print("I have {0:03d} cats".form I have 006
at(6)) cats
print("I have {0:f} cats".format I have
(6)) 6.000000 cats
print("I have {0:.2f} cats".form I have 6.00
at(6)) cats
Read more from https://pyformat.info/
f-Strings

6
Strings
• What is the difference between the
following two instructions:
– Go write your name on the board
– Go write "your name" on the board

7
Strings
• Text consists of characters: letters,
numbers, punctuation, spaces, and so on.
• A string is a sequence of characters. For
example, the string "Hello" is a sequence
of five characters.

8
String Types
• strings in print statements
– print("Hello")
• A string can be stored in a variable
• greeting = "Hello”
• String can be accessed
– print(greeting)

9
String Literal
• A string literal denotes a particular string
(such as "Hello").
• String literals are specified by enclosing a
sequence of characters within a matching
pair of either single or double quotes.
– print("This is a string.", 'So is this.')
– message = 'He said "Hello"'

10
String Length
• The number of characters in a string is
called the length of the string.
– the length of "Harry" is 5.
• Compute the length of a string
– length = len("World!") # length is 6
• A string of length 0 is called the empty
string. It contains no characters and is
written as "" or ''.
11
String Concatenation
• Given two strings, such as "Harry" and
"Morgan", you can concatenate them to
one long string. In Python, you use the +
operator to concatenate two strings.
– firstName = "Harry"
– lastName = "Morgan"
– name = firstName + lastName
– results in the string
• "HarryMorgan"

12
String Concatenation
• name = firstName + " " + lastName
– "Harry Morgan”
• When the expression to the left or the right
of a + operator is a string, the other one
must also be a string or a syntax error will
occur.

13
String Repetition
• You can also produce a string that is the
result of repeating a string multiple times.
• For example,
– dashes = "-" * 50
• A string of any length can be repeated
using the * operator.
– message = "Echo..."
– print(message * 5)
will result
– Echo...Echo...Echo...Echo...Echo… 14
Converting Between Numbers
and Strings
• name = "Agent " + 1729 # Error: Can only
concatenate strings
• Now try this:
– id = 1729
– name = "Agent " + str(id)
• The str function can also be used to
convert a floating-point value to a string.

15
Converting Between Numbers
and Strings
• Conversely, to turn a string containing a
number into a numerical value, use the int
and float functions:
– id = int("1729")
– price = float("17.29")

16

You might also like