
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Get the numeric prefix of given string
Suppose we have a string which contains numbers are the beginning. In this article we will see how to get only the numeric part of the string which is fixed at the beginning.
With isdigit
The is digit function decides if the part of the string is it digit or not. So we will use takewhile function from itertools to join each part of the string which is a digit.
Example
from itertools import takewhile # Given string stringA = "347Hello" print("Given string : ",stringA) # Using takewhile res = ''.join(takewhile(str.isdigit, stringA)) # printing resultant string print("Numeric Pefix from the string: \n", res)
Output
Running the above code gives us the following result −
Given string : 347Hello Numeric Pefix from the string: 347
with re.sub
Using the regular expression module re we can create a pattern to search for the digits only. The search will only find the digits at the beginning of the string.
Example
import re # Given string stringA = "347Hello" print("Given string : ",stringA) # Using re.sub res = re.sub('\D.*', '', stringA) # printing resultant string print("Numeric Pefix from the string: \n", res)
Output
Running the above code gives us the following result −
Given string : 347Hello Numeric Pefix from the string: 347
With re.findall
The findall function works in a similar manner as a girl accept that we use a plus sign instead of *.
Example
import re # Given string stringA = "347Hello" print("Given string : ",stringA) # Using re.sub res = ''.join(re.findall('\d+',stringA)) # printing resultant string print("Numeric Pefix from the string: \n", res)
Output
Running the above code gives us the following result −
Given string : 347Hello Numeric Pefix from the string: 347
- Related Articles
- Python – Sort given list of strings by numeric part of string
- Python – Sort given list of strings by part the numeric part of string
- Program to find smallest string with a given numeric value in Python
- Print the longest prefix of the given string which is also the suffix of the same string in C Program.
- Python – Split Numeric String into K digit integers
- Check if suffix and prefix of a string are palindromes in Python
- Find the longest sub-string which is prefix, suffix and also present inside the string in Python
- Python Regex to extract maximum numeric value from a string
- Python – Substitute prefix part of List
- Return a boolean array where the string element in array begins with a given prefix but test begin and end in Python
- How to get the mean of columns that contains numeric values of a dataframe in Pandas Python?
- K Prefix in Python
- Python - Prefix sum list
- Program to get final string after shifting characters with given number of positions in Python
- Python Numeric Types
