Found 27104 Articles for Server Side Programming

How can I parse a numeric string to its corresponding float value?

Pythonista
Updated on 25-Feb-2020 11:22:35

80 Views

Python’s number conversion function float() converts an integer to float with fractional part as 0. It also parses a string with valid representation of float number to a float object.>>> float('1.11') 1.11 >>> float(1) 1.0 >>> float('1') 1.0 >>> float('1.1e-2') 0.011

How to convert an integer to a octal string in Python?

Pythonista
Updated on 25-Feb-2020 11:22:01

340 Views

We have use oct() function from Python’s library to convert any integer to its octal equivalent number. We get a string having octal representation>>> oct(100) '0o144' >>> oct(0x10) '0o20' >>> oct(10) '0o12'

How to convert an integer to a hexadecimal string in Python?

Pythonista
Updated on 25-Feb-2020 11:21:28

516 Views

We can use built in hex() function to convert any integer to its hexadecimal representation.>>> hex(100) '0x64' >>> hex(4095) '0xfff' >>> hex(31) '0x1f'

How to convert an integer to an ASCII value in Python?

Pythonista
Updated on 25-Feb-2020 11:16:05

1K+ Views

ASCII character associated to an integer is obtained by chr() function. The argument for this function can be any number between 0 to 0xffff>>> chr(0xaa) 'ª' >>> chr(0xff) 'ÿ' >>> chr(200) 'È' >>> chr(122) 'z'

How to convert an integer to a unicode character in Python?

Pythonista
Updated on 09-Sep-2023 15:26:00

3K+ Views

Python library's chr() function converts Unicode character associated to any integer which is between 0 an 0x10ffff.>>> chr(36) '$' >>> chr(97) 'a' >>> chr(81) 'Q'

How to convert a single character to its integer value in Python?

Pythonista
Updated on 25-Feb-2020 11:14:47

508 Views

Each character is associated with an ASCII value which is a unique number. It is obtained by ord() function.>>> ord('A') 65 >>> ord('+') 43 >>> ord(' ')

How we can create a dictionary from a given tuple in Python?

Pythonista
Updated on 25-Feb-2020 11:14:12

312 Views

We can use zip() function to produce an iterable from two tuple objects, each corresponding to key and value items and then use dict() function to form dictionary object>>> T1=('a','b','c','d') >>> T2=(1,2,3,4) >>> dict((x,y) for x,y in zip(t1,t2))Dictionary comprehension syntax can also be used to construct dictionary object from two tuples>>> d={k:v for (k,v) in zip(T1,T2)} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

How do we convert a string to a set in Python?

Pythonista
Updated on 25-Feb-2020 11:12:57

528 Views

Python’s standard library contains built-in function set() which converts an iterable to set. A set object doesn’t contain repeated items. So, if a string contains any character more than once, that character appears only once in the set object. Again, the characters may not appear in the same sequence as in the string as set() function has its own hashing mechanism>>> set("hello") {'l', 'h', 'o', 'e'}

How to convert an object x to a string representation in Python?

Malhar Lathkar
Updated on 24-Feb-2020 09:54:15

200 Views

Most commonly used str() function from Python library returns a string representation of object.>>> no=100 >>> str(no) '100' >>> L1=[1,2,3,4] >>> str(L1) '[1, 2, 3, 4]' >>> d={'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> str(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}"However, repr() returns a default and unambiguous representation of the object, where as str() gives an informal representation that may be readable but may not be always unambiguous.>>> str(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}" >>> repr(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}" >>> repr(L1) '[1, 2, 3, 4]' >>> repr(no) '100'

How to create a complex number in Python?

Malhar Lathkar
Updated on 24-Feb-2020 10:04:52

129 Views

Complex number is made up of real and imaginary parts. Real part is a float number, and imaginary part is any float number multiplied by square root of -1 which is defined as j.>>> no=5+6j >>> no.real 5.0 >>> no.imag 6.0 >>> type(no) The resulting object is of complex data type. Python library also has complex() function, which forms object from two float arguments>>> no=complex(5,6) >>> no (5+6j) >>> no.real 5.0 >>> no.imag 6.0 >>> type(no)

Advertisements