0% found this document useful (0 votes)
56 views3 pages

Formatting Strings: Input Input

Formatting strings allows you to format values in a string using placeholders {} and the format() method. You can format different data types like strings, numbers, booleans by specifying the index or keyword of the values in the placeholders. Format specifiers like :.2f can be used to format floating point numbers to a specific number of decimal places. Percentage formatting and f-strings provide alternative ways to format values in strings.

Uploaded by

medrek
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)
56 views3 pages

Formatting Strings: Input Input

Formatting strings allows you to format values in a string using placeholders {} and the format() method. You can format different data types like strings, numbers, booleans by specifying the index or keyword of the values in the placeholders. Format specifiers like :.2f can be used to format floating point numbers to a specific number of decimal places. Percentage formatting and f-strings provide alternative ways to format values in strings.

Uploaded by

medrek
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/ 3

FORMATTING STRINGS

Built-in Python function or method.


Adds a string to an empty "{ }"

In [2]:

student = input("Enter student's name: ")


grade = input("Enter students grade: ")

"The student's name: {}, grade is {}".format(student, grade)

Enter student's name: Michael


Enter students grade: A-
Out[2]:
"The student's name: Michael, grade is A-"

In [3]:

student

Out[3]:
'Michael'

In [5]:

semester1 = eval(input("Enter semester1 grade: "))

semester2 = eval(input("Enter semester2 grade: "))

final_grades = "The total is {}".format((semester1 + semester2))


final_grades
Enter semester1 grade: 55
Enter semester2 grade: 77
Out[5]:
'The total is 132'

In [11]:

animals = ["lions", "zebras", "elephants"]

safari = "I saw a {}, {} and {}"


safari.format(animals[0], animals[1], animals[2])

Out[11]:
'I saw a lions, zebras and elephants'

In [12]:

safari.format(*animals)
Out[12]:
'I saw a lions, zebras and elephants'

In [19]:
eval("{} + {} + {}".format(10, 20, 30))

Out[19]:
60

In [47]:

eval("{1} * {1} + {1}".format(*[10, 20, 30]))


Out[47]:
420

In [22]:
"%d + %d = %d" % (10, 20, (10 + 20))
Out[22]:
'10 + 20 = 30'

In [192]:

"%s / %s = %s" % (10, 20, (10/20))


Out[192]:
'10 / 20 = 0.5'

In [193]:
"s / %s = %d"

Out[193]:
's / %s = %d'

In [80]:
word = "awesome"
"The length of this word {} is {}".format(word, len(word))
Out[80]:
'The length of this word awesome is 7'

In [174]:
"days left are {num: .1f}".format(num = 300/9)
Out[174]:
'days left are 33.3'

In [176]:
num = 300/9
f"days left are {num: .2f}"
Out[176]:
Out[176]:
'days left are 33.33'

In [177]:
"{} {} {}".\
format("a", "b", "c")
Out[177]:
'a b c'

In [181]:
eval(f"{10 / 22 : .5f}") + 100

Out[181]:
100.45455

In [63]:
p1 = "Python" ; p2 = "older"

f"""This is a docstring that uses {p1} 3.6 a


nd can't be used for
{p2} versions!""".replace("\n", "").upper()

Out[63]:
"THIS IS A DOCSTRING THAT USES PYTHON 3.6 AND CAN'T BE USED FOR OLDER VERSI
ONS!"

You might also like