Python - Extract Percentages from String
Last Updated :
14 Mar, 2023
Given a String, extract all the numbers that are percentages.
Input : test_str = 'geeksforgeeks 20% is 100% way to get 200% success' Output : ['20%', '100%', '200%'] Explanation : 20%, 100% and 200% are percentages present. Input : test_str = 'geeksforgeeks is way to get success' Output : [] Explanation : No percentages present.
Method #1 : Using findall() + regex
In this, we employ appropriate regex having "%" symbol in suffix and use findall() to get all occurrences of such numbers from String.
Python3
# Python3 code to demonstrate working of
# Extract Percentages from String
# Using regex + findall()
import re
# initializing strings
test_str = 'geeksforgeeks is 100 % way to get 200 % success'
# printing original string
print("The original string is : " + str(test_str))
# getting required result from string
res = re.findall('\d*%', test_str)
# printing result
print("The percentages : " + str(res))
OutputThe original string is : geeksforgeeks is 100% way to get 200% success
The percentages : ['100%', '200%']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using re.sub() + split()
In this, we perform split of all words, and then from words that have %, we remove all non-numeric strings. This can be buggy in cases, we have different ordering of % and numbers in string.
Python3
# Python3 code to demonstrate working of
# Extract Percentages from String
# Using re.sub() + split()
import re
# initializing strings
test_str = 'geeksforgeeks is 100 % way to get 200 % success'
# printing original string
print("The original string is : " + str(test_str))
# extracting words
temp = test_str.split()
# using
res = []
for sub in temp:
if '%' in sub:
# replace empty string to all non-number chars
res.append(re.sub(r'[^\d, %]', '', sub))
# printing result
print("The percentages : " + str(res))
OutputThe original string is : geeksforgeeks is 100% way to get 200% success
The percentages : ['100%', '200%']
Time complexity: O(n), where n is the length of the test_list. The re.sub() + split() takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Similar Reads
Python | Extract words from given string In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation
4 min read
Python - Extract digits from given string We need to extract the digit from the given string. For example we are given a string s=""abc123def456gh789" we need to extract all the numbers from the string so the output for the given string will become "123456789" In this article we will show different ways to extract digits from a string in Py
2 min read
Python - Extract numbers from list of strings We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method us
2 min read
Python - Extract String till Numeric Given a string, extract all its content till first appearance of numeric character. Input : test_str = "geeksforgeeks7 is best" Output : geeksforgeeks Explanation : All characters before 7 are extracted. Input : test_str = "2geeksforgeeks7 is best" Output : "" Explanation : No character extracted as
5 min read
Python | Sort a list of percentage Given a list of percentage, write a Python program to sort the given list in ascending order. Let's see different ways to do the task. Code #1: Chops '%' in string and convert it into float. Python3 # Python code to sort list of percentage # List initialization Input =['2.5 %', '6.4 %', '91.6 %', '1
3 min read