String Formatting¶
Anatomy of Strings¶
Single Quote
Double Quotes
Single Quote
VS
Double Quote (Problems & Advantage)Triple Quotes(also called docstring)
New Line
Tab
str1 = 'Hello Bangladesh'
str2 = "Rahim's Name"
print(str2)
Rahim's Name
str3 = 'Rahim's Name
print(str3)
File "<ipython-input-31-32cfe8b954b7>", line 2
str3 = 'Rahim's Name
^
SyntaxError: invalid syntax
Escape Sequence & Comments¶
str4 = "Badol's Name"
print(str4)
Badol's Name
doc = """doctsring"""
print(doc)
doctsring
"""This is a comment"""
'This is a comment'
print("I Love \nBangladesh")
I Love
Bangladesh
I Love
Bangladesh
print("I Love\tBangladesh")
I Love Bangladesh
I Love Bangladesh
Printing Message / String Formatting¶
Printing any variable without message¶
Syntax¶
print(name_of_variable)
num1 = 10
print(num1)
10
pi = 3.1416
print(pi)
3.1416
p = 4.231236700912
print(round(p, 2))
4.23
num4 = 3 + 2j
print(num4)
(3+2j)
name = "John Doe"
print(name)
John Doe
Printing variable with message¶
Syntax¶
print("message",variable)
pi = 3.1416
print("The value of pi is", pi)
The value of pi is 3.1416
g = 9.8
print("The value of g is", g)
The value of g is 9.8
a = 10
print(a, "is a integer value")
10 is a integer value
name = "John Doe"
print("Hello", name)
Hello John Doe
String Interpolation / f-Strings (Python 3.6+)¶
Syntax¶
print(f"Message {variable_name}")
pi = 3.1416
print(f"The value of pi is {pi}")
The value of pi is 3.1416
name = "John Doe"
print(f"My name is {name}")
My name is John Doe
name = "John"
age = 23
print(f"My name is {name} and I'm {age}")
My name is John and I'm 23
a = 10
b = 20
total = a+b
print(f"The sum of {a} and {b} is {total}")
The sum of 10 and 20 is 30