How to remove empty strings from a list of strings in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 18:47:18
You can use filter to filter out empty strings. The first argument of filter is lambda that will tell you if the string is empty. You can pass this lambda to be None, bool, len or a function that checks if the string is empty. Here are some of the ... Read More

How to process escape sequences in a string in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 18:43:50
There are two ways to go about unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:>>> import ast >>> a = '"Hello, \\nworld"' >>> print ast.literal_eval(a) Hello, ... Read More

Why substring slicing index out of range works in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 18:23:14
Slicing outside the bounds of a sequence (at least for built-ins) doesn't cause an error. Indexing returns a single item, but slicing returns a subsequence of items. So when you try to index a nonexistent value, there's nothing to return; but when you slice a sequence outside of bounds, you ... Read More

How to format a floating number to fixed width in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 18:10:12
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 4 digits on the right of the decimal, you can use the following:>>> x = 12.35874 >>> ... Read More

How to sort a list of strings in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 18:07:19
To sort a list in place, i.e., sort the list itself and change ordering in that list itself, you can use sort() on the list of strings. For example, >>> a = ["Hello", "My", "Followers"] >>> a.sort() >>> print a ['Followers', 'Hello', 'My']If you want to keep the original list ... Read More

How to concatenate a string with numbers in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 17:50:24
To concatenate a string with numbers, you need to cast the numbers to string using str(number). For example, >>> a = "string" >>> b = 1 >>> print a + str(b) string1In Python 2, you can also use backtick(``) to surround the number and get the same result with numbers ... Read More

How to trim down non printable characters from a string in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 17:44:27
If you have only ASCII characters and want to remove the non-printable characters, the easiest way is to filter out those characters using string.printable. For example, >>> import string >>> filter(lambda x: x in string.printable, '\x01string') stringThe 0x01 was not printed as it is not a printable character. If you ... Read More

How do I un-escape a backslash-escaped string in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 17:32:36
There are two ways to go about unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:>>> import ast >>> a = '"Hello, \\nworld"' >>> print ast.literal_eval(a) Hello, ... Read More

How can I concatenate str and int objects in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 17:04:05
To concatenate a string with numbers, you need to cast the numbers to string using str(number). For example, >>> a = "string" >>> b = 1 >>> print a + str(b) string1In Python 2, you can also use backtick(``) to surround the number and get the same result with numbers ... Read More

How to generate random strings with upper case letters and digits in Python?

Rajendra Dharmkar
Published on 13-Dec-2017 16:00:56
You can use random.choice(list_of_choices) to get a random character. Then loop over this and get a list and finally join this list to get a string. The list of choices here is Upper case letters and digits. For example:import string import random def get_random_string(length):     random_list = []   ... Read More
Advertisements