Advertisement
Functions/Method | Description |
---|---|
str() | This function formats an object passed to it and returns its human-readable form. |
repr() | This function returns an interpreter-readable form of an object passed to it. |
format() | This method returns a formatted string value and it is used to format specified value(s) passed to it and insert them inside the string's placeholder. This method is only available to string objects. |
# Python - Example of str() function
# Creating a skeletal class by using the pass keyword
class A:
pass;
# Creating different types of objects
i = 100
s1= 19.567
s2 = 'Hello, world.'
s3 = 'Poliśh'
# Calling str() with an int object.
print(str(i))
# Calling str() with an floating-point object.
print(str(s1))
# Calling str() with a string object.
print(str(s2))
# Calling str() with another string object.
print(str(s3))
# Calling str() by passing it a result of concatenating two string objects
print(str('Hello' + 'Goodbye'))
# Calling str() by passing it two number objects
print(str(100/200.5))
#Creating an object of class A
ob = A()
#Calling str() with the object of class A
print(str(ob))
100
19.567
Hello, world.
Poliśh
HelloGoodbye
0.49875311720698257
<__main__.A object at 0x0000000002ED3E10>
# Python - Example of repr() function
# Creating a skeletal class by using the pass keyword
class A:
pass;
# Creating different types of objects
i = 100
s1= 19.567
s2 = 'Hello, world.'
s3 = 'Poliśh'
# Calling repr() with an int object.
print(repr(i))
# Calling repr() with an floating-point object.
print(repr(s1))
# Calling repr() with a repring object.
print(repr(s2))
# Calling repr() with another repring object.
print(repr(s3))
# Calling repr() by passing it a result of concatenating two repring objects
print(repr('Hello' + 'Goodbye'))
# Calling repr() by passing it two number object
print(repr(100/200.5))
#Creating an object of class A
ob = A()
#Calling repr() with the object of class A
print(repr(ob))
100
19.567
'Hello, world.'
'Poliśh'
'HelloGoodbye'
0.49875311720698257
<__main__.A object at 0x0000000002EBEEF0>
Advertisement
# Python - Example of format() function
# Creating different types of objects
i = 100
s1= 19.567
s2 = 'never give up'
s3 = 'think positive'
# Calling the format() method of string to format the output
# Index 0 refers to i and index 1 refers to s1
print('{0}, {1}'.format(i, s1))
# Calling the format() method of string to format the output
# Index 0 refers to i and index 1 refers to s1
print('{1}, {0}'.format(i, s1))
# Calling the format() method of string to format the output
# First blank {} refers to the first argument passed to format() method
# Second blank {} refers to the second argument passed to the format() method
print('In life, you should {} and always {}'.format(s2,s3))
# Calling the format() method of string to format the output
# blank {} refers to the argument passed to format() method, which is itself a string
print('{} is blue'.format('Sky'))
# Calling the format() method of string to format the output
# First {} refers to the argument - weather and extracts its value 'hot'
# Second {} refers to the argument - love and extracts its value 'rain'
print('Today, its a {weather} day and I hope it {love}'.format(weather='hot', love='rain'))
# Calling the format() method of string to format the output
# First blank {} refers to the first argument passed to the format() method
# Second {} refers to the argument - color and extracts its value 'green'
print('{} are {color}'.format('Grapes', color='green'))
100, 19.567
19.567, 100
In life, you should never give up and always think positive
Sky is blue
Today, its a hot day and I hope it rain
Grapes are green
[index-position]:[width][format-specifier]
Format Specifier | Description |
---|---|
c | to display an argument as a character. |
f | to display an argument as a float. |
s | to display an argument as a string. |
o | to display an argument as an octal. |
d | to display an argument as a decimal. |
x | to display an argument as a hexadecimal with a lowercase character after 9. |
X | to display an argument as a hexadecimal with an uppercase character after 9. |
# Python - Example of format() function
# Calling the format() method of string to format the output
# using format specifier f i.e. float with first element and width of 2
# because there was only one argument to format() method, hence we didn't have to specify its index.
print('{:.2f}'.format(10.37489))
# Calling the format() method of string to format the output
# using format specifier f i.e. float with index 0 element and width of 0
# using format specifier f i.e. float with index 1 element and width of 6
print('{0:.0f}, {1:.6f}'.format(10.37489, 19.567))
# Calling the format() method of string to format the output
# Index 0 refers to i and index 1 refers to s1
print('{0:f}, {1:.5f}'.format(10.37489, 19.567))
# Calling the format() method of string to format the output
# using format specifier f i.e. float with index 0 element and width of 2
# using format specifier f i.e. float with index 1 element and width of 5
print('{0:.2f}, {1:.5f}'.format(100,200))
# Calling the format() method of string to format the output
# using format specifier d i.e. int with index 0 element
# using format specifier f i.e. float with index 1 element and width of 4
print('{0:d}, {1:.4f}'.format(100,200))
# Calling the format() method of string to format the output
# using format specifier b i.e. binary, to format the output in binary
print('{0:b}'.format(10))
# Calling the format() method of string to format the output
# using format specifier o i.e. octal, to format the output in octal
print('{0:o}'.format(10))
# Calling the format() method of string to format the output
# using format specifier d i.e. decimal, to format the output in decimal
print('{0:d}'.format(10))
# Calling the format() method of string to format the output
# using format specifier o i.e. hexadecimal, to format the output in hexadecimal
print('{0:X}'.format(10))
# Calling the format() method of string to format the output
# using format specifier o i.e. character, to format the output in character
print('{0:c}'.format(65))
# Calling the format() method of string to format the output
# using format specifier s i.e. string, to format the output in string
print('{0:s}'.format('Hello, how do you do?'))
10.37
10, 19.567000
10.374890, 19.56700
100.00, 200.00000
100, 200.0000
1010
12
10
A
A
Hello, how do you do?
[index-position]:[shifting-character][width][format-specifier]
Shifting character | Description |
---|---|
< | Left shifts the value. |
> | Right shifts the value. |
s | Center shifts the value. |
# Python - Example of format() function
# Calling the format() method of string to format the output
# Setting the width of value to 4
print('{:4}'.format(10))
# Right shifting the value and setting the width to 8
print('This is a {:>8} day'.format('good'))
# Don't have to specify the index of the element when there is only one argument to format()
# Left shifting the value by width 9
print('This is a {:<9} day'.format('good'))
# Right shifting the 1st index element by width 7
# Left shifting the zeroth index value by width 10
print('An {1:>7} a day keeps a {0:<10} away'.format('doctor', 'apple'))
# Right shifting the 1st index value by width 7
# Center shifting the 2nd index value by width 10
# Left shifting the zeroth index value by width 10
print('A {:<7} without laughter {:^10} a day {:>10} !'.format('day', 'is', 'wasted'))
# Left shifting the value by width 7
print('This is a {:<7} day'.format('good'))
# Right shifting the value and setting the width to 7
print('{:>8}'.format(10.567))
# Right shifting the value and setting the width to 10
print('{:<10} '.format(10))
# Left shifting the value and setting the width to 10
print('{:>10} '.format(10))
# Center shifting the value and setting the width to 10
print('{:^10} '.format(10))
10
This is a good day
This is a good day
An apple a day keeps a doctor away
A day without laughter is a day wasted !
This is a good day
10.567
10
10
10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement