Python Print statements :
Concept 1:
print("Hello")
Hello
print ( "hello" )
hello
print(" HELLO ") #space before H
HELLO
space inside quotation give space in output message, whereas space outside quotation, python neglects and outputs are same.
Concept 2:
print ("hello","!!!")
hello !!!
print ("hello ","!!!") # space after O
hello !!!
print ("hello", " !!!") # space before !
hello !!!
print ("hello" + "!!!")
hello!!!
print ("hello " + "!!!") #space after O
hello !!!
Comma gives a space between words in a message whereas + symbols adds the words and leaves no space between two words. while using + symbol, need a add a space along with the word to get the space in the output message.
Concept 3:
print ("hi", "everyone", "welcome", "all")
hi everyone welcome all
# comma seperates words with a space while printing
print ("hi" + "everyone" + "welcome" + "all")
hieveryonewelcomeall
# + symbol adds the words together before printing
print ("hi " + "everyone " + "welcome " + "all.")
hi everyone welcome all.
# space addded to each word and then message printed
Concept 4: sep means seperator
print ("hello", "world", sep=" ")
hello world
print ("hello", "world", sep="")
helloworld
print ("hello", "world", sep="\n") #next line
hello
world
print ("hello", "world", sep="\t") # tab
hello world
print ("hello", "hi", "everyone", "welcome", sep="all")
helloallhialleveryoneallwelcome
print ("hello", "hi", "everyone", "welcome", sep=" all ")
hello all hi all everyone all welcome
print("hello\bworld") #backspace
hellworld
#it prints hello, then gives a backspace, which deletes o then world is printed.
concept 5: adding string & Integer
print ("abc", 123)
abc 123
print ("abc"+ str(123)) #type casting
abc123
Concept 6: String multiplication
print ("Sham "*10)
Sham Sham Sham Sham Sham Sham Sham Sham Sham Sham
Concept 7:
printing names
name = "sham"
print (name)
sham
print ("name")
name
print multiple items and formatting
age = 35
name = "sathesh"
city = "newyork"
print ("Name:", name, "Age:", age, "City:", city)
Name: sathesh Age: 35 City: newyork
print (f"Name: {name}, Age: {age}, city: {city}")
Name: sathesh, Age: 35, city: newyork
print ("Name: {name}") #if f not mentioned
Name: {name}
Concept 8:
join the string
my_first_name="shyamala"
my_last_name="chandramohan"
print(my_first_name, my_last_name, "!")
shyamala chandramohan !
print(my_first_name+ " "+ my_last_name+ " !")
shyamala chandramohan !
Concept 9:
Print numbers and expression
print(12345)
12345
print(1+2+3)
6
print("""This is a
... multiline
... string""")
This is a
multiline
string
Concept 10:
Combine strings and variables using +
temperature = 22.5
print("The temperature is " + str(temperature) + " degrees Celsius.")
Concept 11:
Use the .format() method for string formatting
age = 35
name = "sathesh"
city = "newyork"
print("Name: {}, Age: {}, City: {}".format(name, age, city))
Name: sathesh, Age: 35, City: newyork
Concept 11:
to print raw string (to print a path name, use r"
print(r"C:\Users\Name")
print("C:\\Users\\Name")
C:\Users\Name
C:\Users\Name
Concept 12:
print the value of pi (3.14159) rounded to two decimal places in the format The value of pi is approximately 3.14?
PI=3.14159
print(f"{PI:.2f}")
3.14