How do I insert a variable into a string in Python?
If you want to insert a variable into a string in python
there are several methods to insert a variable into a string.
- Concatenation Method.
- format method
- % operator
- Concatenation Method.
var = “shubham”
print(“Hello” + var + “welcome to my tutorial”)
2. format specifier
var1 = “shubham”
age = 24
var2 = “Hello {} you are {} years old”.format(var1,age)
print(var2)
3. % operator
var = “shubham”
var2 = “hello %s welcome back”%var
print(var2)
I hope it helps you.
Comments
Post a Comment