text
stringlengths
200
200k
source
stringclasses
2 values
# How do you test that a Python function throws an exception? ## Question How does one write a unittest that fails only if a function doesn't throw an expected exception? ## Answer I use doctest[1] almost everywhere because I like the fact that I document and test my functions at the same time. Have a look at this c...
stackoverflow-qa
# How do you test that a Python function throws an exception? ## Question How does one write a unittest that fails only if a function doesn't throw an expected exception? ## Answer I just discovered that the Mock library provides an assertRaisesWithMessage() method (in its unittest.TestCase subclass), which will chec...
stackoverflow-qa
# How do you test that a Python function throws an exception? ## Question How does one write a unittest that fails only if a function doesn't throw an expected exception? ## Answer Since Python 2.7 you can use context manager to get a hold of the actual Exception object thrown: import unittest def broken_function()...
stackoverflow-qa
# How do you test that a Python function throws an exception? ## Question How does one write a unittest that fails only if a function doesn't throw an expected exception? ## Answer from: http://www.lengrand.fr/2011/12/pythonunittest-assertraises-raises-error/ First, here is the corresponding (still dum :p) function ...
stackoverflow-qa
# How do you test that a Python function throws an exception? ## Question How does one write a unittest that fails only if a function doesn't throw an expected exception? ## Answer You can build your own contextmanager to check if the exception was raised. import contextlib @contextlib.contextmanager def raises(exc...
stackoverflow-qa
# How do you test that a Python function throws an exception? ## Question How does one write a unittest that fails only if a function doesn't throw an expected exception? ## Answer How do you test that a Python function throws an exception? How does one write a test that fails only if a function doesn't throw ...
stackoverflow-qa
# Is there an inverse function for time.gmtime() that parses a UTC tuple to seconds since the epoch? ## Question python's time module seems a little haphazard. For example, here is a list of methods in there, from the docstring: time() -- return current time in seconds since the Epoch as a float clock() -- return CP...
stackoverflow-qa
# Is there an inverse function for time.gmtime() that parses a UTC tuple to seconds since the epoch? ## Question python's time module seems a little haphazard. For example, here is a list of methods in there, from the docstring: time() -- return current time in seconds since the Epoch as a float clock() -- return CP...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How do I efficiently filter computed values within a Python list comprehension? ## Question The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However,...
stackoverflow-qa
# How to pass all Visual Studio 2008 "Macros" to Python script? ## Question [NOTE: This questions is similar to but not the same as this one.] Visual Studio defines several dozen "Macros" which are sort of simulated environment variables (completely unrelated to C++ macros) which contain information about the build i...
stackoverflow-qa
# Python Date Comparisons ## Question I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: if (datetime.now() - self.timestamp) > 100 # Where 100 is either seconds or minutes This generates a type error. What is the proper way ...
stackoverflow-qa
# Python Date Comparisons ## Question I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: if (datetime.now() - self.timestamp) > 100 # Where 100 is either seconds or minutes This generates a type error. What is the proper way ...
stackoverflow-qa
# Python Date Comparisons ## Question I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: if (datetime.now() - self.timestamp) > 100 # Where 100 is either seconds or minutes This generates a type error. What is the proper way ...
stackoverflow-qa
# Python Date Comparisons ## Question I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: if (datetime.now() - self.timestamp) > 100 # Where 100 is either seconds or minutes This generates a type error. What is the proper way ...
stackoverflow-qa
# Request UAC elevation from within a Python script? ## Question I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe "as administator" and then run my script, it works fine. This makes sense since User Acco...
stackoverflow-qa
# Request UAC elevation from within a Python script? ## Question I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe "as administator" and then run my script, it works fine. This makes sense since User Acco...
stackoverflow-qa
# Request UAC elevation from within a Python script? ## Question I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe "as administator" and then run my script, it works fine. This makes sense since User Acco...
stackoverflow-qa
# Request UAC elevation from within a Python script? ## Question I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe "as administator" and then run my script, it works fine. This makes sense since User Acco...
stackoverflow-qa
# Request UAC elevation from within a Python script? ## Question I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe "as administator" and then run my script, it works fine. This makes sense since User Acco...
stackoverflow-qa
# Request UAC elevation from within a Python script? ## Question I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe "as administator" and then run my script, it works fine. This makes sense since User Acco...
stackoverflow-qa
# Style - When to serialize a Django model Instance: signals vs model's save method ## Question I plan to serialize a Django model to XML when it's saved or updated. (The XML's going to be imported into a flash movie). Is it better to listen for a post_save() or pre_save() signal and then perform the serial...
stackoverflow-qa
# Do I have to cause an ValueError in Python ## Question I have this code: chars = #some list try: indx = chars.index(chars) except ValueError: #doSomething else: #doSomethingElse I want to be able to do this because I don't like knowfully causing Exceptions: chars = #some list indx = chars.index(char...
stackoverflow-qa
# Do I have to cause an ValueError in Python ## Question I have this code: chars = #some list try: indx = chars.index(chars) except ValueError: #doSomething else: #doSomethingElse I want to be able to do this because I don't like knowfully causing Exceptions: chars = #some list indx = chars.index(char...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Showing the stack trace from a running Python application ## Question I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Prin...
stackoverflow-qa
# Translate algorithmic C to Python ## Question I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures. An example would be a regular expression library. Translation tool ...
stackoverflow-qa
# Translate algorithmic C to Python ## Question I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures. An example would be a regular expression library. Translation tool ...
stackoverflow-qa
# Translate algorithmic C to Python ## Question I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures. An example would be a regular expression library. Translation tool ...
stackoverflow-qa
# Translate algorithmic C to Python ## Question I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures. An example would be a regular expression library. Translation tool ...
stackoverflow-qa
# Regex to remove conditional comments ## Question I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments. I would also like to avoid using the .*? notation if possible. The text is foo <!--[if IE]> <style type="text/css"> u...
stackoverflow-qa
# Regex to remove conditional comments ## Question I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments. I would also like to avoid using the .*? notation if possible. The text is foo <!--[if IE]> <style type="text/css"> u...
stackoverflow-qa
# Regex to remove conditional comments ## Question I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments. I would also like to avoid using the .*? notation if possible. The text is foo <!--[if IE]> <style type="text/css"> u...
stackoverflow-qa
# Regex to remove conditional comments ## Question I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments. I would also like to avoid using the .*? notation if possible. The text is foo <!--[if IE]> <style type="text/css"> u...
stackoverflow-qa
# Regex to remove conditional comments ## Question I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments. I would also like to avoid using the .*? notation if possible. The text is foo <!--[if IE]> <style type="text/css"> u...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Is there a difference between `==` and `is` in Python? ## Question My Google-fu has failed me. In Python, are the following two tests for equality equivalent (ha!)? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing ...
stackoverflow-qa
# Concurrent Access to RRD (RRDTool) ## Question I am using RRDTool (http://oss.oetiker.ch/rrdtool/) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script. My problem is that the script is multithreaded and each thread updates the RRD at a pretty rapid pace. S...
stackoverflow-qa
# Concurrent Access to RRD (RRDTool) ## Question I am using RRDTool (http://oss.oetiker.ch/rrdtool/) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script. My problem is that the script is multithreaded and each thread updates the RRD at a pretty rapid pace. S...
stackoverflow-qa
# Concurrent Access to RRD (RRDTool) ## Question I am using RRDTool (http://oss.oetiker.ch/rrdtool/) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script. My problem is that the script is multithreaded and each thread updates the RRD at a pretty rapid pace. S...
stackoverflow-qa
# Running subversion under apache and mod_python ## Question My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message: svn: Can't open file '/root/.subversion/servers'...
stackoverflow-qa
# Efficiently match multiple regexes in Python ## Question Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with: import re import sys class Token(object): """ A simple Token structure. Contains the token type, value...
stackoverflow-qa
# Efficiently match multiple regexes in Python ## Question Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with: import re import sys class Token(object): """ A simple Token structure. Contains the token type, value...
stackoverflow-qa
# Efficiently match multiple regexes in Python ## Question Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with: import re import sys class Token(object): """ A simple Token structure. Contains the token type, value...
stackoverflow-qa
# Efficiently match multiple regexes in Python ## Question Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with: import re import sys class Token(object): """ A simple Token structure. Contains the token type, value...
stackoverflow-qa
# Efficiently match multiple regexes in Python ## Question Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with: import re import sys class Token(object): """ A simple Token structure. Contains the token type, value...
stackoverflow-qa
# Efficiently match multiple regexes in Python ## Question Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with: import re import sys class Token(object): """ A simple Token structure. Contains the token type, value...
stackoverflow-qa
# Run Pylons controller as separate app? ## Question I have a Pylons app where I would like to move some of the logic to a separate batch process. I've been running it under the main app for testing, but it is going to be doing a lot of work in the database, and I'd like it to be a separate process that will be runn...
stackoverflow-qa
# Run Pylons controller as separate app? ## Question I have a Pylons app where I would like to move some of the logic to a separate batch process. I've been running it under the main app for testing, but it is going to be doing a lot of work in the database, and I'd like it to be a separate process that will be runn...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# Which is more preferable to use in Python: lambda functions or nested functions ('def')? ## Question I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another ...
stackoverflow-qa
# How do I use django.core.urlresolvers.reverse with a function reference instead of a named URL pattern? ## Question In my urls.py file, I have: from myapp import views ... (r'^categories/$', views.categories) Where categories is a view function inside myapp/views.py. No other URLconf lines reference views.categor...
stackoverflow-qa
# How do I use django.core.urlresolvers.reverse with a function reference instead of a named URL pattern? ## Question In my urls.py file, I have: from myapp import views ... (r'^categories/$', views.categories) Where categories is a view function inside myapp/views.py. No other URLconf lines reference views.categor...
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer x = [1, 10, 100] for i in x: print '%02d' % i results: 01 10 100 Read more information about string formatting using % in the doc...
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer Here you are: print "%02d" % (1,) Basically % is like printf or sprint. Tags: python, string-formatting
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer Use a format string - http://docs.python.org/lib/typesseq-strings.html For example: python -c 'print "%(num)02d" % {"num":5}' Tags: pyt...
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer In Python 3, you would use the format() string method: for i in (1, 10, 100): print('{num:02d}'.format(num=i)) or using the built-i...
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer You can use zfill: print str(1).zfill(2) print str(10).zfill(2) print str(100).zfill(2) prints: 01 10 100 Tags: python, string-for...
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer Or this: print '{0:02d}'.format(1) Tags: python, string-formatting
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer The pythonic way to do this: str(number).rjust(string_width, fill_char) This way, the original string is returned unchanged if its leng...
stackoverflow-qa
# Display number with leading zeros ## Question Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits, i.e.: 01 10 100 ## Answer print '{:02}'.format(a) print '{:02}'.format(b) print '{:02}'.format(c) prints: 01 10 100 Tags: python, string-formatting
stackoverflow-qa