How to Upload a File in Python Tkinter

Upload a file in Python Tkinter: a Tkinter window listing three uploaded files with their sizes in a Treeview

To upload a file in Python Tkinter, call filedialog.askopenfilename() from a button, then copy the chosen file to an uploads folder with shutil.copy2() or send it to a server with requests.post(). The dialog only returns a path: My test setup was Python 3.12.5 (Tk 8.6), Pillow 12.3.0 and requests 2.34.2 on Windows 11. The windows … Read more >>

How to Call a Function Within a Function in Python

Call a function within a function in Python: Command Prompt output of an order total built from helper functions

To call a function within a function in Python, write its name with parentheses inside the other function’s body, like subtotal(prices). Python looks the name up when the outer function runs, not when it’s defined: Each listing below was run on Python 3.12.5; the Command Prompt captures are from those same runs, and the numbers … Read more >>

How to Swap Characters in a String Using Python

Swap characters in a string in Python: Command Prompt output fixing a swapped-letter typo in Denver with list and join

To swap characters in a string in Python, turn the string into a list, swap the two positions with tuple unpacking, and join it back. Strings are immutable, so you always build a new string: The outputs below come from Python 3.12.5 in a Windows Command Prompt, and the examples are small US place names, … Read more >>

How to Pad a String with Zeros in Python

Python pad string with zeros: Command Prompt output of zfill padding order IDs to five digits

The quickest way to pad a string with zeros in Python is str.zfill(width): it adds zeros on the left until the string is width characters long and keeps a leading + or – in front. For zeros on the right, use ljust(width, “0”): I checked every result with Python 3.12.5 and pandas 3.0.6, using order … Read more >>

How to Split a Long String into Multiple Lines in Python

Split a long string into multiple lines in Python: Command Prompt output of textwrap.wrap at 40 characters

To split a long string into multiple lines in Python, use textwrap.wrap(text, width), which returns a list of lines broken at spaces, or textwrap.fill() for one string with line breaks. If the text already has newlines, splitlines() splits on them: My sample text is a short store notice, and the outputs come from Python 3.12.5. … Read more >>

How to Escape Curly Braces in Python Format Strings

Escape curly braces in Python: Command Prompt output of doubled braces in an f-string and str.format

To escape curly braces in Python format strings, double them: {{ prints { and }} prints }. The rule is the same for f-strings and str.format(), and single braces stay placeholders: Every snippet here was run on Python 3.12.5, plus Python 3.14 for the new template strings near the end. We’ll cover doubled braces, putting … Read more >>

How to Convert a Map to an Array in TypeScript

TypeScript map to array: Command Prompt output of a Map converted to entries, keys and values arrays

To convert a Map to an array in TypeScript, spread it: […map] gives you an array of [key, value] pairs. For just the keys or the values, spread map.keys() or map.values(), or use Array.from(): The examples use a small coffee shop inventory, and they were compiled and run with TypeScript 7.0.2 on Node.js v22.22.2. We’ll … Read more >>

How to Create a Map from an Array in TypeScript

TypeScript create map from array: Command Prompt output of a Map built from an array of customer objects

You can create a Map from an array in TypeScript by passing an array of [key, value] pairs to new Map(). For an array of objects, turn each object into a pair with map() first, and TypeScript infers the key and value types for you: These snippets were checked with TypeScript 7.0.2 with Node.js v22.22.2 … Read more >>

How to Sum an Array in TypeScript (Numbers and Objects)

TypeScript sum array: Command Prompt output of summing an array of objects with reduce

To get the sum of an array in TypeScript, call reduce() with an initial value of 0: it adds every element to a running total and returns a number. Always pass that 0, or an empty array will throw: I compiled each example with TypeScript 7.0.2 and Node.js v22.22.2 using strict mode, and the outputs … Read more >>

UnboundLocalError: Local Variable Referenced Before Assignment in Python

UnboundLocalError in Python: Command Prompt traceback for cannot access local variable where it is not associated with a value

UnboundLocalError (“local variable referenced before assignment”) means a function reads a variable that Python has decided is local, before that local variable has been given a value. It almost always happens because the function assigns to a name that also exists outside it. Newer versions of Python, including the Python 3.12.5 I used for every … Read more >>