FinishedTheBasics
This commit is contained in:
29
LearnTheBasics/5_StringFormatting.py
Normal file
29
LearnTheBasics/5_StringFormatting.py
Normal file
@ -0,0 +1,29 @@
|
||||
#Let's say you have a variable called "name" with your user name in it, and you would then like to print(out a greeting to that user.)
|
||||
|
||||
# This prints out "Hello, John!"
|
||||
name = "John"
|
||||
print("Hello, %s!" % name)
|
||||
|
||||
# This prints out "John is 23 years old."
|
||||
name = "John"
|
||||
age = 23
|
||||
print("%s is %d years old." % (name, age))
|
||||
|
||||
# This prints out: A list: [1, 2, 3]
|
||||
mylist = [1,2,3]
|
||||
print("A list: %s" % mylist)
|
||||
|
||||
# Here are some basic argument specifiers you should know:
|
||||
# %s - String (or any object with a string representation, like numbers)
|
||||
# %d - Integers
|
||||
# %f - Floating point numbers
|
||||
# %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
|
||||
# %x/%X - Integers in hex representation (lowercase/uppercase)
|
||||
|
||||
# Exercise
|
||||
# You will need to write a format string which prints out the data using the following syntax: Hello John Doe. Your current balance is $53.44.
|
||||
|
||||
data = ("John", "Doe", 53.44)
|
||||
format_string = "Hello"
|
||||
|
||||
print(format_string, "%s, %s, %f" % data)
|
Reference in New Issue
Block a user