Comments:
Comments can be used to explain Python code. It can be used to make the code more readable. It can also be used to prevent execution when testing code.
Comments starts with a #, and Python will ignore them:
print("This is a print statement \nThis is a new line")
print("Hello World!!!") #Printing Hello World
Output:
This is a print statement
This is a new line
Hello World!!!
Escape Sequence:
To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.
Example:
#using double quote
print('This is\' a \"print statement\" and this is a new line')
#using single quote
print('This is\' a \"print statement\" and this is a new line')
Output:
This is' a "print statement" and this is a new line
Other escape characters used in Python:
- Single Quote ( \' ) - \' convert single quote into normal character
txt = 'It\'s alright.'
print(txt) #output: It's alright.
- Backslash ( \ ) - \ backslash convert special character into normal character
txt = "This will insert one \\ (backslash)."
print(txt) # This will insert one \ (backslash).
- New Line ( \n ) - \n is also an escape character which creates a new line.
txt = "Hello\nWorld!" #Due to \n 'World' will print in new line.
print(txt)
# Output:
# Hello
# World!
- Carriage Return ( \r ) - \r will just work as you have shifted your cursor to the beginning of the string or line.
txt = "Hello\rWorld!"
print(txt)
# Output:
# Hello
# World!
- Tab ( \t ) - \t creates tab space between words. It is majorly used for alignments.
txt = "Hello\tWorld!"
print(txt) #output: Hello World!
- Backspace ( \b ) - \b removes space between characters.
#This example erases one character (backspace):
txt = "Hello \bWorld!"
print(txt)
#output: HelloWorld!
- Form Feed ( \f ): The form feed character is a control character in ASCII that is represented by the escape sequence \f. It is used to instruct a printer or other output device to advance to the next page or form. In Python, you can print the form feed character using the following syntax: print('\f')
- Octal value ( \ooo ):
#A backslash followed by three integers will result in a octal value:
txt = "\110\145\154\154\157"
print(txt) #ouput: Hello
txt = 'It\'s alright.'
print(txt) #output: It's alright.
txt = "This will insert one \\ (backslash)."
print(txt) # This will insert one \ (backslash).
txt = "Hello\nWorld!" #Due to \n 'World' will print in new line.
print(txt)
# Output:
# Hello
# World!
txt = "Hello\rWorld!"
print(txt)
# Output:
# Hello
# World!
txt = "Hello\tWorld!"
print(txt) #output: Hello World!
#This example erases one character (backspace):
txt = "Hello \bWorld!"
print(txt)
#output: HelloWorld!
#A backslash followed by three integers will result in a octal value:
txt = "\110\145\154\154\157"
print(txt) #ouput: Hello
The octal values used in the code are as follows:
\110 - represents the octal value 110, which corresponds to the ASCII character ‘H’.
\145 - represents the octal value 145, which corresponds to the ASCII character ‘e’.
\154 - represents the octal value 154, which corresponds to the ASCII character ‘l’.
\154 - represents the octal value 154, which corresponds to the ASCII character ‘l’.
\157 - represents the octal value 157, which corresponds to the ASCII character ‘o’.
So it prints "Hello"
\110 - represents the octal value 110, which corresponds to the ASCII character ‘H’.
\145 - represents the octal value 145, which corresponds to the ASCII character ‘e’.
\154 - represents the octal value 154, which corresponds to the ASCII character ‘l’.
\154 - represents the octal value 154, which corresponds to the ASCII character ‘l’.
\157 - represents the octal value 157, which corresponds to the ASCII character ‘o’.
So it prints "Hello"
- Hex value ( \xhh):
#A backslash followed by an 'x' and a hex number represents a hex value:
txt = "\x48\x65\x6c\x6c\x6f"
print(txt) #ouput: Hello
#A backslash followed by an 'x' and a hex number represents a hex value:
txt = "\x48\x65\x6c\x6c\x6f"
print(txt) #ouput: Hello
The hexadecimal values used in the code are as follows:
\x48 represents the hex value 48, which corresponds to the ASCII character ‘H’.
\x65 represents the hex value 65, which corresponds to the ASCII character ‘e’.
\x6c represents the hex value 6c, which corresponds to the ASCII character ‘l’.
\x6c represents the hex value 6c, which corresponds to the ASCII character ‘l’.
\x6f represents the hex value 6f, which corresponds to the ASCII character ‘o’.
So it prints "Hello"
\x48 represents the hex value 48, which corresponds to the ASCII character ‘H’.
\x65 represents the hex value 65, which corresponds to the ASCII character ‘e’.
\x6c represents the hex value 6c, which corresponds to the ASCII character ‘l’.
\x6c represents the hex value 6c, which corresponds to the ASCII character ‘l’.
\x6f represents the hex value 6f, which corresponds to the ASCII character ‘o’.
So it prints "Hello"
Print Statement:
The print() function prints the specified message to the screen, or other standard output device.
The message can be a string, or any other object, the object will be converted into a string before written to the screen.
The message can be a string, or any other object, the object will be converted into a string before written to the screen.
Syntax:
print(object(s), sep=separator, end=end, file=file, flush=flush)
Parameter Description object(s) Any object, and as many as you like. Will be converted to string before printed sep='separator' Optional. Specify how to separate the objects, if there is more than one. Default is ' ' end='end' Optional. Specify what to print at the end. Default is '\n' (line feed) file Optional. An object with a write method. Default is sys.stdout flush Optional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False
Example 01 - If only object is provided:
print("Hello World" ) #output: Hello World
Example 02 - If object and separator are provided:
print("Hey", 6, 7, sep"~" ) #output: Hey~6~7
Example 03 - If object, separator and end are provided:
print("Hey", 6, 7, sep"~", end="009\n")
print("Python")
#output:
# Hey~6~7~009
# Python
To Connect: CodingSplash
Comments
Post a Comment