I want to replace whitespace with underscore in a string to create nice URLs. So that for example: Should become I am using Python with Django. Can this be solved using regular expressions? Answer You don’t need regular expressions. Python has a built-in string method that does what you need:
How do I pass a variable by reference?
Are parameters passed by reference or by value? How do I pass by reference so that the code below outputs ‘Changed’ instead of ‘Original’? See also: Why can a function modify some arguments as perceived by the caller, but not others? Answer Arguments are passed by assignment. The rationale behind this is twofold: the parameter passed in is actually a
How do I wait for a pressed key?
How do I make my python script wait until the user presses any key? Answer In Python 3, use input(): In Python 2, use raw_input(): This only waits for the user to press enter though. On Windows/DOS, one might want to use msvcrt. The msvcrt module gives you access to a number of functions in the Microsoft Visual C/C++ Runtime
Mark data as sensitive in python
I need to store a user’s password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as “sensitive”, so it’s not saved anywhere by a debugger? Answer Edit I have made a solution that uses ctypes (which
Shuffling a list of objects
How do I shuffle a list of objects? I tried random.shuffle: But it outputs: Answer random.shuffle should work. Here’s an example, where the objects are lists: Note that shuffle works in place, and returns None. More generally in Python, mutable objects can be passed into functions, and when a function mutates those objects, the standard is to return None (rather
How to get string objects instead of Unicode from JSON
I’m using Python 2 to parse JSON from ASCII encoded text files. When loading these files with either json or simplejson, all my string values are cast to Unicode objects instead of string objects. The problem is, I have to use the data with some libraries that only accept string objects. I can’t change the libraries nor update them. Is
How do I use raw_input in Python 3?
In Python 2: In Python 3, I get an error: NameError: name ‘raw_input’ is not defined Answer Starting with Python 3, raw_input() was renamed to input(). From What’s New In Python 3.0, Builtins section second item.
How can I partition (split up, divide) a list based on a condition?
I have some code like: The goal is to split up the contents of mylist into two other lists, based on whether or not they meet a condition. How can I do this more elegantly? Can I avoid doing two separate iterations over mylist? Can I improve performance by doing so? Answer How can I do this more elegantly? That
How do I reverse a string in Python?
There is no built in reverse function for Python’s str object. What is the best way of implementing this method? If supplying a very concise answer, please elaborate on its efficiency. For example, whether the str object is converted to a different object, etc. Answer Using slicing: Slice notation takes the form [start:stop:step]. In this case, we omit the start
How do I get the last element of a list?
How do I get the last element of a list? Which way is preferred? Answer some_list[-1] is the shortest and most Pythonic. In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, all the way down to some_list[-len(some_list)], which gives