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 >>

Python Variable Scope: Local, Global, Nonlocal and LEGB

Python variable scope: Command Prompt output showing the LEGB rule with local, enclosing, global and built-in names

Scope decides where a Python variable can be seen: a name created inside a function is local, and a name at the top of a file is global. Python looks names up in Local, Enclosing, Global, then Built-in scope, the LEGB rule. These examples ran on Python 3.12.5, and the screenshots come straight from the … Read more >>

How to Use TypeScript Const Arrays?

Const Array in TypeScript

In this tutorial, I will explain how to use the const arrays in TypeScript. I will also show you some examples of using const arrays in TypeScript. What is a Const Array in TypeScript? Let me first explain what a const array is in TypeScript. The const keyword in TypeScript is used to declare variables … Read more >>

How to Use Join() Method to Combine Array of Strings in TypeScript?

Join() Method to Combine Array of Strings in TypeScript

In this tutorial, I will explain how to use TypeScript’s join() method to combine an array of strings into a single string. This method is particularly useful when you need to create a comma-separated list, format strings for display, or generate URLs from an array of parameters. What Is The Join() Method in TypeScript The … Read more >>