CyberChants has Published 5 Answers

How to split string on whitespace in Python?

CyberChants

CyberChants

Updated on 30-Sep-2019 07:33:51

You can usesplit() from the string class. The default delimiter for this method is whitespace, i.e., when called on a string, it'll split that string at whitespace characters. For example: >>>"Please split this string".split() ['Please', 'split', 'this', 'string'] Regex can also be used to solve this problem. You can call the re.split() ... Read More

What is the difference between encode/decode in Python?

CyberChants

CyberChants

Updated on 30-Sep-2019 07:32:24

To represent a unicode string as a string of bytes is known as encoding. To convert a string of bytes to a unicode string is known as decoding. You typically encode a unicode string whenever you need to use it for IO, for instance transfer it over the network, or ... Read More

How to find all possible permutations of a given string in Python?

CyberChants

CyberChants

Updated on 30-Sep-2019 07:31:30

To find all possible permutations of a given string, you can use the itertools module which has a useful method called permutations(iterable[, r]). This method return successive r length permutations of elements in the iterable as tuples. In order to get all the permutations as string, you'll need to iterate over ... Read More

How to convert string to binary in Python?

CyberChants

CyberChants

Updated on 30-Sep-2019 07:29:16

To convert a string to binary, you need to iterate over each character and convert it to binary. Then join these characters together in a single string. You can use format(ord(x), 'b') to format the character x as binary. For example:>>>st = "hello world" >>>' '.join(format(ord(x), 'b') for x in ... Read More

How to display a float with two decimal places in Python?

CyberChants

CyberChants

Updated on 30-Sep-2019 07:26:54

You can use string formatting to format floating point numbers to a fixed width in Python.For example, if you want the decimal points to be aligned with width of 12 characters and 2 digits on the right of the decimal, you can use the following: >>>x = 12.35874 >>>print "{:12.2f}".format(x)   ... Read More

1
Advertisements