Class 12 : Python String Inbuilt Functions 5

bool()

bool() converts a value to Boolean. If there is some value it return True and if value is blank then it is False.

>>> bool(0.5)
True
>>> bool(“”)
False
>>> bool(“hello”)
True
>>> bool(“empty”)
True
>>>
>>> bool(34)
True
>>>

chr()

chr(): This function returns the character form of the ASCII value.

A-Z:65 to 90
a-z:97 to 122
0-9: 48 to 57

>>> chr(65)
‘A’

>>> chr(97)
‘a’

>>> chr(90)
Z

>>> chr(122)
z

>>> chr(48)
‘0’

ord()

The function ord() returns an integer that represents the Unicode (ACSII Value) of character.

>>> ord(‘A’)
65
>>> ord(“A”)
65
>>> ord(‘Z’)
90
>>> ord(‘a’)
97
>>> ord(‘z’)
122
>>>

id() Function

id() returns an object’s identity.

>>> a=10
>>> b=20
>>> c=30
>>> id(a)
2563040504400
>>> id(b)
2563040504720
>>> id(c)
2563040505040
>>> a=10
>>> b=10
>>> id(a)
2563040504400
>>> id(b)
2563040504400
>>>

int()

int() converts a value to an integer.

>> int(‘7’)
7
>>> a=”10″
>>> b=”20″
>>> c=a+b
>>> c
‘1020’
>>> c=int(a)+int(b)
>>> c
30
>>>

>>> a=19.67
>>> print(a)
19.67
>>> print(int(a))
19

str()

str() takes an argument and returns the string equivalent of it.

>>> str(‘Hello’)
‘Hello’

>>> str(7)
‘7’

>>> str([1,2,3])
‘[1, 2, 3]’

type()

This function returning the data type of the object.

>>> print(type(23))
<class ‘int’>
>>> print(type(12.56))
<class ‘float’>
>>> print(type((1,2,3)))
<class ‘tuple’>
>>> print(type([1,2,3]))
<class ‘list’>
>>> print(type({1:’a’,2:’b’,3:’c’}))
<class ‘dict’>
>>>

sorted()

Function sorted() prints out a sorted version of an iterable. It does not, however, alter the iterable.

>>> sorted(‘Python’)
[‘P’, ‘h’, ‘n’, ‘o’, ‘t’, ‘y’]

>>> sorted([1,3,2])
[1, 2, 3]