Category: Strings
Remove last N characters from string in Python
In this article we will discuss different ways to delete last N characters from a string i.e. using slicing or for loop or regex. Remove last N character from string by slicing In python, we can use slicing to select a specific range of characters in a string i.e. Advertisements str[start:end] It returns the characters …
Python: Remove all numbers from string
In this article we will discuss different ways to remove characters except digits from string in Python. Remove all numbers from string using regex Python’s regex module provides a function sub() i.e. Advertisements re.sub(pattern, repl, string, count=0, flags=0) It returns a new string. This new string is obtained by replacing all the occurrences of the …
Python: Remove characters from string by regex & 4 other ways
In this article we will discuss different ways to delete single or multiple characters from string in python either by using regex() or translate() or replace() or join() or filter(). Remove characters from string using regex Python’s regex module provides a function sub() i.e. Advertisements re.sub(pattern, repl, string, count=0, flags=0) It returns a new string. …
Python: Remove characters from string by regex & 4 other waysRead More
Remove first N Characters from string in Python
In this article we will discuss ways to remove first N characters from a string either by using slicing or regex or simple for loop. Use slicing to remove first N character from string in python In python, we can use slicing to select a specific range of characters in a string i.e. Advertisements str[start:end] …
Python: Replace a character in a string
In this article, we will discuss different ways to replace a character in string in python. Overview: Replace all occurrences of a character in string using replace() Replace the first two occurrences of a character in string Replace a character in a string using regex Replace a character in a string using for loop Replace …
Python: Replace multiple characters in a string
In this article, we will discuss different ways to replace multiple characters in a string in Python. Table of Contents: Replace multiple characters in a string using the replace() Replace multiple characters in a string using the translate () Replace multiple characters in a string using regex Replace multiple characters in a string using for …
Python String – replace() method
In this article, we will learn to use string class’s replace() function to replace sub-strings in a string in Python. In Python, the String class (Str) provides a method replace() to substitute the sub-strings in a string. But as strings are immutable in Python, this function returns a copy of the calling string object with …
Python: String to int
In this article, we will discuss how to convert a number in a string to an int object. Table of Contents Advertisements Syntax of int() function. Convert string to int in python. Convert hex string to int in python. Convert binary string to int. Convert number string with comma to int. Python provides a function …
Convert string to float in python
In this article, we will discuss how to convert a number string to a float object. Table of Contents Syntax of float() function Convert string to float object Convert number string with commas to float object Python provides a function to convert a number string into a floating-point number. Advertisements Syntax of float() function float(object) …
Python: Replace character in string by index position
In this article, we will discuss how to replace a character in a string at a specific position. Then we will also see how to replace multiple characters in a string by index positions. Table of Contents Use Python string slicing to replace nth character in a string Python function to replace a character in …
Python: Replace character in string by index positionRead More