Python String

Python Int to String with Leading Zeros

To convert an integer i to a string with leading zeros so that it consists of 5 characters, use the format string f'{i:05d}’. The d flag in this expression defines that the result is a decimal value. The str(i).zfill(5) accomplishes the same string conversion of an integer with leading zeros. Challenge: Given an integer number. …

Python Int to String with Leading Zeros Read More »

How to Generate Text Automatically With Python? A Guide to the DeepAI API

Do you want to enrich your Python script with powerful text-generation capabilities? You’re in the right place! What does it do? I just discovered DeepAI’s API that automatically generates a body of text, given a sentence fragment or topic keyword. How can it be used? You can use this as a basis to generate text …

How to Generate Text Automatically With Python? A Guide to the DeepAI API Read More »

__str__ vs __repr__ In Python

Summary: The key goal of __str__ and __repr__ is to return a string representation of a Python object. The way they represent the string object differentiates them. str() & __str()__ return a printable/readable string representation of an object which is focused on the end-user. repr() & __repr()__ return a string representation of an object that is a valid Python object, something you can …

__str__ vs __repr__ In Python Read More »

How To Eliminate All The Whitespace From A String?

In this article, you’ll learn the ultimate answer to the following question: How To Eliminate All The Whitespace From A String—on Both Ends, and In-Between Words? Summary: Use the string methods join(), split(), strip(), rstrip(), lstrip() and or replace()—in specific combinations—to remove any whitespace in a given string. The simplest way to remove all whitespaces …

How To Eliminate All The Whitespace From A String? Read More »

Slice Notation – A Simple Illustrated Guide

Summary: Slicing is a Python concept to extract a subsequence from a string or list—that lies within a start and stop index range. There are two syntactical ways to define a slice. (1) The extended slice notation makes use of a colon : in string_name[start:stop:step]. (2) The slice() constructor defines the index range in string_name[slice(start:stop:step)]. …

Slice Notation – A Simple Illustrated Guide Read More »

How To Format A String That Contains Curly Braces In Python?

Summary: Use one of the following methods to format strings that contain curly braces: Use double curly braces {{}} Use the old string formatting, i.e. the % operator Use the JSON Library Use Template Strings Problem: Given a string literal with curly braces; how to format the string and ensure that the curly braces are …

How To Format A String That Contains Curly Braces In Python? Read More »

56 Python One-Liners to Impress Your Friends

This is a running document in which I’ll answer all questions regarding the single line of Python code. It’s based on my interactive collection here but without the slow videos and embedded code shells. Let’s get started! Python One Line If Else You can use a simple if statement in a single line of code. …

56 Python One-Liners to Impress Your Friends Read More »

Python Unicode Encode Error

Summary: The UnicodeEncodeError generally occurs while encoding a Unicode string into a certain coding. Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode(utf-8) and decode(utf-8) functions accordingly in your …

Python Unicode Encode Error Read More »

How to Solve Python “TypeError: ‘int’ object is not iterable”?

It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code. We’ll look at a specific example of such an error: “typeerror: ‘int’ object is not iterable”. Exercise: Run …

How to Solve Python “TypeError: ‘int’ object is not iterable”? Read More »

How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9]. Use a regex module and the split() method along with …

How To Split A String And Keep The Separators? Read More »