Manogna has Published 66 Answers

How to retrieve Python module path?

Manogna

Manogna

Updated on 30-Sep-2019 08:51:02

For a pure python module you can find the location of the source files by looking at the module.__file__. For example,  >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py  Many built-in modules, however, are written in C, and therefore module.__file__ points to a .so file (there is no module.__file__ on Windows), and therefore, ... Read More

How do I unload (reload) a Python module?

Manogna

Manogna

Updated on 30-Sep-2019 08:50:27

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "importmoduleName" without exiting the script. It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again. For example, ... Read More

How to wrap long lines in Python?

Manogna

Manogna

Updated on 30-Sep-2019 08:29:05

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately. The preferred place ... Read More

How to check whether a string starts with XYZ in Python?

Manogna

Manogna

Updated on 30-Sep-2019 08:27:53

Python has a method startswith(string) in the String class. This method accepts a prefix string that you want to search and is called on a string object. You can call this method in the following way:>>>'hello world'.startswith('hell') True >>>'hello world'.startswith('nope') False There is another way to find if a string ends ... Read More

How to convert a string to dictionary in Python?

Manogna

Manogna

Updated on 30-Sep-2019 08:27:13

We can use ast.literal_eval() here to evaluate the string as a python expression. It safely evaluates an expression node or a string containing a Python expression.The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. For example:  >>>import ast ... Read More

How to replace the last occurrence of an expression in a string in Python?

Manogna

Manogna

Updated on 30-Sep-2019 08:25:24

This problem can be solved by reversing the string, reversing the string to be replaced, replacing the string with reverse of string to be replaced with and finally reversing the string to get the result. You can reverse strings by simple slicing notation - [::-1]. To replace the string you can ... Read More

How to correctly sort a string with a number inside in Python?

Manogna

Manogna

Updated on 30-Sep-2019 08:24:05

This type of sort in which you want to sort on the basis of numbers within string is called natural sort or human sort. For example, if you have the text:['Hello1', 'Hello12', 'Hello29', 'Hello2', 'Hello17', 'Hello25'] Then you want the sorted list to be:['Hello1', 'Hello2', 'Hello12', 'Hello17', 'Hello25', 'Hello29'] and not:['Hello1', 'Hello12', ... Read More

How can I remove the ANSI escape sequences from a string in python?

Manogna

Manogna

Updated on 30-Sep-2019 07:34:57

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re.sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]'.For example, import re def escape_ansi(line):     ansi_escape =re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')   ... Read More

How to get the length of a string in Python?

Manogna

Manogna

Updated on 27-Sep-2019 12:07:57

Python has a method called len() that gives us the length of any composite object. To get the length of a string, just pass the string to the len() call. For example,print(len('abcdefghijklmnopqrstuvwxyz'))OUTPUT26

How can I write a try/except block that catches all Python exceptions?

Manogna

Manogna

Updated on 27-Sep-2019 11:29:24

It is a general thumb rule that though you can catch all exceptions using code like below, you shouldn’t:try:     #do_something() except:     print "Exception Caught!"However, this will also catch exceptions like KeyboardInterrupt we may not be interested in. Unless you re-raise the exception right away – we ... Read More

Advertisements