Different Ways to Create Strings in Python

creating and storing strings in python

When I first started working with Python more than ten years ago, I quickly realized how important strings were. Almost every project I worked on involved text in some way. I’ve explored many different ways to create strings in Python. Some methods are simple and quick, while others give you more flexibility. In this tutorial, … Read more >>

jQuery Check if Element Has Class

jQuery Check Element Has Class

When working with dynamic web applications, you’ll often need to check whether an HTML element contains a specific CSS class. jQuery provides several powerful methods to accomplish this task efficiently. In this comprehensive guide, we’ll explore all the ways to check if an element has a class using jQuery, complete with practical examples and best … Read more >>

Booleans in Python: True, False and How to Check Them

Python booleans: the Command Prompt showing that the string False evaluates as True

A boolean in Python is a value that is either True or False, and nothing else: Both must be capitalised. Lowercase true is just an undefined name, so it raises a NameError. The trap worth knowing before anything else: bool(“False”) is True, because any non-empty string is truthy. Everything on this page was run on … Read more >>

Remove the Last Character From a String in Python

Python remove the last character from a string: the Command Prompt showing how a zero count empties the string

To remove the last character from a string in Python, slice up to -1: Two traps follow from that. Writing text[:-n] with n as a variable empties the string when n is zero, and rstrip removes more characters than you expect. Both are demonstrated below, on Python 3.12.5. Removing the last character with slicing A … Read more >>

Reverse a String in Python: Slicing, Loops and Recursion

Python reverse a string: the Command Prompt comparing the speed of slicing, reversed and a manual loop

To reverse a string in Python, slice it with a step of -1: There is no reverse() method on strings, because strings cannot be changed in place. Every approach below builds a new string. All timings and output come from real runs on Python 3.12.5. Reversing a string with slicing The slice takes three numbers, … Read more >>

NumPy sum(): Axis, keepdims, dtype and Sum of Squares

Python NumPy sum: the Command Prompt comparing three ways to compute a sum of squares

np.sum adds up the values in a NumPy array, either all of them or along one axis: The axis argument is what people come for, and the rule is short: the axis you name is the one that disappears from the result. All figures below are real runs on Python 3.12.5, NumPy 2.5.3. Using np.sum … Read more >>

PyTorch nn.Linear: Shapes, Weights and the Multiply Error

PyTorch nn.Linear: the Command Prompt showing that the weight shape is out_features by in_features

nn.Linear is PyTorch’s fully connected layer. It takes a number of input features and produces a number of output features: Two details save a lot of debugging. The weight is stored as (out_features, in_features), and the layer only transforms the last dimension of your input. Shapes, weights and errors below are real output from Python … Read more >>

Matplotlib Set Y Axis to Log Scale (base, symlog and zeros)

Matplotlib set y axis log scale: a linear plot beside the same data on a logarithmic y axis

To set a Matplotlib y axis to a log scale, call set_yscale on the axes or plt.yscale on the current figure: Two things catch people out. The argument is base, not basey, and any zero or negative value is dropped without a warning. Both are demonstrated below, on Python 3.12.5, Matplotlib 3.11.2. Setting the y … Read more >>

Get the First N Characters of a String in Python

Python get the first n characters of a string: the Command Prompt showing slice syntax in use

To get the first n characters of a string in Python, slice from the start: The useful part is what it doesn’t do. Asking for more characters than the string has returns the whole string rather than raising an error. All output below comes from real runs on Python 3.12.5. Getting the first n characters … Read more >>