Python String

How to Convert a Unicode String to a String Object in Python?

This tutorial will show you how to convert a Unicode string to a string in Python. If you already know about Unicode, you can skip the following background section and dive into the problem right away. Background Unicode A bit about Unicode from Wikipedia. Unicode is a character encoding standard that includes characters from almost …

How to Convert a Unicode String to a String Object in Python? Read More »

Python repr() Function — A Helpful Guide with Example

Python’s built-in repr(obj) function returns the standard string representation of the provided object. This often includes the type and memory address of the object—for lack of further information. For example, the returned string representation may be ‘<main.Car object at 0x000001F66D11DBE0>’ for a custom object of type Car. The function internally calls the method obj.__repr__() which …

Python repr() Function — A Helpful Guide with Example Read More »

Python str() Function

Python’s built-in str(x) function converts the object x to a string using the x.__str__() method or, if non-existent, the repr(x) built-in function to obtain the string conversion. Syntax str() Syntax: str(object) # –> Most common case: convert an object to a string str(object=b”, encoding=’utf-8′, errors=’strict’) # –> Not so common case: Converts a bytes or …

Python str() Function Read More »

How to Find Multiple Occurrences of a String Within a String in Python?

Problem Formulation: Given a longer string and a shorter string. How to find all occurrences of the shorter string in the longer one? Consider the following example: Longer string: ‘Finxters learn Python with Finxter’ Shorter string: ‘Finxter’ Result: [‘Finxter’, ‘Finxter’] Optionally, you may also want to print the positions where the shorter string arise in …

How to Find Multiple Occurrences of a String Within a String in Python? Read More »

How to Split a Byte String into Lines?

Problem Formulation: Given a byte string that contains new-line characters ‘\n’. How to split the byte string into a list of lines? Example: You want to transform the byte string b’your\nbyte\nstring’ into the list of byte strings [b’your’, b’byte’, b’string’] using b’\n’ as a newline separator. Given: b’your\nbyte\nstring’ Goal: [b’your’, b’byte’, b’string’] Solution: To split …

How to Split a Byte String into Lines? Read More »

How to Print an Integer with Commas as Thousands Separators in Python?

Problem Formulation: Given an integer number. How to convert the integer to a string representation for printing or other use that has thousand separators? Example: Given an integer number 1000000. You want the string representation ‘1,000,000’. Method 1: f-Strings Using the modern f-strings is, in my opinion, the most Pythonic solution to add commas as …

How to Print an Integer with Commas as Thousands Separators in Python? Read More »

How to Remove Everything After the Last Character in a String?

Problem Formulation Given string s, and character c. How to remove all characters in s after the first occurrence of c? Example Given: – string s = ‘hello world’, and – empty space character c = ‘ ‘. Desired result: ‘hello’ Method 1: string.index() + slicing To remove everything after the first occurrence of character …

How to Remove Everything After the Last Character in a String? Read More »

How to Split a String Between Numbers and Letters?

Problem Formulation: Given a string of letters and numbers. How to split the string into substrings of either letters or numbers by using the boundary between a letter and a number and vice versa. Examples: Have a look at the following examples of what you want to accomplish. ‘111A222B333C’ —> [‘111’, ‘A’, ‘222’, ‘B’, ‘333’, …

How to Split a String Between Numbers and Letters? Read More »

Python ord() Function

The Python ord() function takes a character (=string of length one) as an input and returns the Unicode number of this character. For example, ord(‘a’) returns the Unicode number 97. The inverse function of ord() is the chr() function, so chr(ord(‘a’)) returns the original character ‘a’. Here are three examples of passed Unicode characters transformed …

Python ord() Function Read More »