Ayse
0
Q:

format python

# Newer f-string format
name = "Foo"
age = 12
print(f"Hello, My name is {name} and I'm {age} years old.")
# output :
# Hello, my name is Foo and I'm 12 years old.
26
string_a = "Hello"
string_b = "Cena"

# Index based:
print("{0}, John {1}"
      .format(string_a, string_b))
# Object based:
print("{greeting}, John {last_name}"
      .format(greeting=string_a, last_name=string_b))
12
# Python3 program to demonstarte 
# the str.format() method 
  
# using format option in a simple string 
print ("{}, A computer science portal for geeks."
                        .format("GeeksforGeeks")) 
  
# using format option for a 
# value stored in a variable 
str = "This article is written in {}"
print (str.format("Python")) 
  
# formatting a string using a numeric constant 
print ("Hello, I am {} years old !".format(18))  
13
print("My name is {0} and I am {1} years old".format(name, age))
6
'{} {}'.format('one', 'two')
1

    txt1 = "My 
    name is {fname}, I'am {age}".format(fname = "John", age = 36)
txt2 = "My 
    name is {0}, I'am {1}".format("John",36)
txt3 = "My 
    name is {}, I'am {}".format("John",36) 
4
'{:10.5}'.format('xylophone')
0
#TO  know this execute  and check
username,password = "GOOGLE","google"
#a. %  first type
print("User name=%s User password=%s"%(username,password))
#b. {}  second type
print("User name={} User password={}".format(username,password
#c. f  third type
print(f"User name={username} User password={password}")
print(f"User name={username} \nUser password={password}"
0
#The {} are replaced by the vairables after .format
new_string = "{} is string 1 and {} is string 2".format("fish", "pigs")
0

New to Communities?

Join the community