How to print double quotes around a variable?

Update : From Python 3.6, you can use f-strings >>> print(f'”{word}”‘) “Some Random Word” Original Answer : You can try %-formatting >>> print(‘”%s”‘ % word) “Some Random Word” OR str.format >>> print(‘”{}”‘.format(word)) “Some Random Word” OR escape the quote character with \ >>> print(“\”%s\”” % word) “Some Random Word” And, if the double-quotes is not … Read more

Why does “%-d”, or “%-e” remove the leading space or zero?

Python datetime.strftime() delegates to C strftime() function that is platform-dependent: The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation. Glibc notes for strftime(3): – … Read more

Please elaborate on the use of `WITH` over `TRY CATCH` in this context

In general, a context manager is free to do whatever its author wants it to do when used. Set/reset a certain system state, cleaning up resources after use, acquiring/releasing a lock, etc. In particular, as Jon already writes, a database connection object creates a transaction when used as a context manager. If you want to … Read more

How to convert a nested list into a one-dimensional list in Python? [duplicate]

You need to recursively loop over the list and check if an item is iterable(strings are iterable too, but skip them) or not. itertools.chain will not work for [1,[2,2,2],4] because it requires all of it’s items to be iterable, but 1 and 4 (integers) are not iterable. That’s why it worked for the second one … Read more

Pathname too long to open?

Regular DOS paths are limited to MAX_PATH (260) characters, including the string’s terminating NUL character. You can exceed this limit by using an extended-length path that starts with the \\?\ prefix. This path must be a Unicode string, fully qualified, and only use backslash as the path separator. Per Microsoft’s file system functionality comparison, the … Read more

String.maketrans for English and Persian numbers

See unidecode library which converts all strings into UTF8. It is very useful in case of number input in different languages. In Python 2: >>> from unidecode import unidecode >>> a = unidecode(u”۰۱۲۳۴۵۶۷۸۹”) >>> a ‘0123456789’ >>> unidecode(a) ‘0123456789’ In Python 3: >>> from unidecode import unidecode >>> a = unidecode(“۰۱۲۳۴۵۶۷۸۹”) >>> a ‘0123456789’ >>> … Read more