Professional Documents
Culture Documents
Statements in Python typically end with a new line. Python does, however,
allow the use of the line continuation character (\) to denote that the line should
continue. For example −
total = item_one + \
item_two + \
item_three
Statements contained within the [], {}, or () brackets do not need to use the
line continuation character. For example −
days = ['Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday']
In Python, the statements are usually written in a single line and the last
character of these lines is newline. To extend the statement to one or more lines
we can use braces {}, parentheses (), square [], semi-colon “;”, and continuation
character slash “\”. we can use any of these according to our requirement in the
code. With the line continuation character, we can explicitly divide a long
statement into numerous lines (\).
Code:
Python3
g = "geeks\
for\
geeks"
print(g)
In the above code if we do not use the continuation characters the code will give
unterminated string literal error.
Output:
geeksforgeeks
Python3
articles"
add = 50 + \
40 - \
52
Output:
Initializing a text using the Explicit multi-line statement A Computer Science
portalfor geeks. It contains well written, well thought and well explained
computer science and programming articles
list = [5,
4, 3, 2, 1
]
add = (50 +
40 -
52)
Output:
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]
A group of individual statements, which make a single code block are called
suites in Python. Compound or complex statements, such as if, while, def, and
class require a header line and a suite.
Header lines begin the statement (with the keyword) and terminate with a colon
(: ) and are followed by one or more lines which make up the suite.
example
if expr1==True:
stmt1
stmt2
elif expr2==True:
stmt3
stmt4
else:
stmt5
stmt6
while expr==True:
stmt1
stmt2
QUOTES IN PYTHON
Quotation symbols are used to create string object in Python. Python recognizes
single, double and triple quoted strings. String literals are written by enclosing a
sequence of characters in single quotes ('hello'), double quotes ("hello") or triple
quotes ('''hello''' or """hello""").
>>> var1='hello'
>>> var1
'hello'
>>> var2="hello"
>>> var2
'hello'
>>> var3='''hello'''
>>> var3
'hello'
>>> var4="""hello"""
>>> var4
'hello'
If it is required to embed double quotes as a part of string, the string itself should
be put in single quotes. On the other hand, if single quoted text is to be embedded,
string should be written in double quotes.
>>> var1='Welcome to "Python training" from Tutorialspoint'
>>> var1
'Welcome to "Python training" from Tutorialspoint'
>>> var2="Welcome to 'Python training' from Tutorialspoint"
>>> var2
"Welcome to 'Python training' from Tutorialspoint"
Python accepts single ('), double (") and triple (''' or """) quotes to denote string
literals, as long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all
the following are legal −
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
Python input() function is used to get input from the user. It prompts for the user
input and reads a line. After reading data, it converts it into a string and returns
that. It throws an error EOFError if EOF is read.
Signature
1. input ([prompt])
Parameters
Return
Here, we are using this function get user input and display to the user as well.
1. # Python input() function example
2. # Calling function
3. val = input("Enter a value: ")
4. # Displaying result
5. print("You entered:",val)
Output:
Enter a value: 45
You entered: 45
The input() method returns string value. So, if we want to perform arithmetic
operations, we need to cast the value first. See the example below.
1. # Python input() function example
2. # Calling function
3. val = input("Enter an integer: ")
4. # Displaying result
5. val = int(val) # casting into string
6. sqr = (val*val) # getting square
7. print("Square of the value:",sqr)
Output:
Enter an integer: 12
Input function
In Python, we have the input() function for taking the user input. The input()
function prompts the user to enter data.It accepts all user input as string. The user
may enter a number or a string but the input() function treats them as strings only.
The syntax for input() is:
input ([Prompt])
Prompt is the string we may like to display on the screen prior to taking the input,
and it is optional. When a prompt is specified, first it is displayed on the screen
after which the user can enter data. The input() takes exactly what is typed from
the keyboard, converts it into a string and assigns it to the variable on left-hand
side of the assignment operator (=). Entering data for the input function is
terminated by pressing the enter key.
Example
The variable fname will get the string ‘Arnab’, entered by the user. Similarly, the
variable age will get the string ‘19’. We can typecast or change the datatype of the
string data accepted from user to an appropriate numeric value. For example, the
following statement will convert
the accepted string to an integer. If the user enters any non-numeric value, an error
will be generated.
Example
#function int() to convert string to integer
>>> age = int( input("Enter your age:"))
Enter your age: 19
>>> type(age)
<class 'int'>
Signature
1. print(object(s), sep=separator, end=end, file=file, flush=flush)
Parameters
object(s): It is an object to be printed. The Symbol * indicates that there may be
more than one object.
flush (optional): If True, the stream is forcibly flushed. The default value of flush
is False.
Return
1. print("Python is programming language.")
2.
3. x = 7
4. # Two objects passed
5. print("x =", x)
6.
7. y = x
8. # Three objects passed
9. print('x =', x, '= y')
Output:
x=7
x=7=y
Explanation:
In the above code, only objects parameter is passed to print() function (in all three
print statements).
The end parameter '\n' (newline character) is used to display output in the next line,
and it is by default. As we can see, each print statement displays output in the new
line.
If the file is saved as sys.stdout, then, the output is printed on the screen.
Here the value of flush is False, so the stream is not forcibly flushed.
The below example use print() with separator and end parameters.
1. x = 7
2. print("x =", x, sep='00000', end='\n\n\n')
3. print("x =", x, sep='0', end='')
Output:
a =000007
a =07