Increment Numeric Strings by K - Python
Last Updated :
18 Jan, 2025
Given the Strings list we have to increment numeric strings in a list by a given integer K
. ( Note that strings that are not numeric remain unchanged )
For example: In the list ["gfg", "234", "is", "98", "123", "best", "4"]
if K = 6
then result will be ["gfg", "240", "is", "104", "129", "best", "10"]
.
Using List Comprehension and isdigit()
This method uses a list comprehension to process each element in the list (li
) and checks if an element is numeric using isdigit(), f
or numeric elements it converts them to integers then increments by K
and converts back to a string before adding to the result list (res
).
Python
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K and keep non-numeric elements unchanged
res = [str(int(s) + K) if s.isdigit() else s for s in li]
print(res)
Output['gfg', '240', 'is', '104', '129', 'best', '10']
Let's take a look at other methods to Increment Numeric Strings by K.
Using isdigit()
This method processes each element in a list (li
) to check if it's numeric using isdigit()
. If it is then the element is converted to an integer and incremented by K
before being added to the result list res
.
Python
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
res = []
for e in li:
if e.isdigit():
res.append(str(int(e) + k))
else:
res.append(e)
print(res)
OutputIncremented Numeric Strings: ['gfg', '240', 'is', '104', '129', 'best', '10']
Using filter()
and map()
Functions
This method separates numeric and non-numeric elements from the list (li
) using the filter()
function then processes numeric elements by converting them to integers and incrementing them by K
before converting back to strings using map()
. Finally it combines the non-numeric elements with the updated numeric ones to form the final result (res
).
Python
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Separate numeric and non-numeric elements, increment numeric elements by K
num1 = list(map(lambda s: str(int(s) + k), filter(str.isdigit, li)))
num2 = list(filter(lambda s: not s.isdigit(), li))
res = num1 + num2
print(res)
Output['240', '104', '129', '10', 'gfg', 'is', 'best']
Explanation: filter()
function identifies numeric elements using str.isdigit
and non-numeric elements using a lambda function then numeric elements are processed by map()
, where each element is converted to an integer, incremented, and converted back to a string.
Using isnumeric()
Method
This method uses the isnumeric()
method to check if a string element is numeric then for numeric elements it converts them to integers and increments by K
before converting back to strings.
Python
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K
res = [str(int(s) + k) if s.isnumeric() else s for s in li]
print(res)
Output['gfg', '240', 'is', '104', '129', 'best', '10']
Using map()
and lambda
This method uses map()
with a lambda
function to check if an element in the list is numeric then for numeric elements it converts them to integers and increments by K
.
Python
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K using map and lambda
res = list(map(lambda s: str(int(s) + k) if s.isdigit() else s, li))
print(res)
Output['gfg', '240', 'is', '104', '129', 'best', '10']
Using try/except
This method uses a try/except block to handle each element then tries to convert the element to an integer and if successful it increments the value by K. Also note when elements can't be converted it catches the ValueError and keeps them unchanged.
Python
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K using try/except
res = []
for s in li:
try:
res.append(str(int(s) + k))
except ValueError:
res.append(s)
print(res)
Output['gfg', '240', 'is', '104', '129', 'best', '10']
Similar Reads
Insert a number in string - Python
We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num
2 min read
Python - Split Numeric String into K digit integers
Given a String, convert it to K digit integers Input : test_str = '457336', K = 2 Output : [45, 73, 36] Explanation : Divided in 2 digit integers. Input : test_str = '457336', K = 3 Output : [457, 336] Explanation : Divided in 3 digit integers. Method #1 : Using int() + slice + loop In this, we iter
5 min read
Python program to Increment Suffix Number in String
Given a String, the task is to write a Python program to increment the number which is at end of the string. Input : test_str = 'geeks006' Output : geeks7 Explanation : Suffix 006 incremented to 7. Input : test_str = 'geeks007' Output : geeks8 Explanation : Suffix 007 incremented to 8. Method #1 : U
5 min read
Python | Check Numeric Suffix in String
Sometimes, while programming, we can have such a problem in which we need to check if any string is ending with a number i.e it has a numeric suffix. This problem can occur in Web Development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using regex This problem
6 min read
Frequency of Numbers in String - Python
We are given a string and we have to determine how many numeric characters (digits) are present in the given string. For example: "Hello123World456" has 6 numeric characters (1, 2, 3, 4, 5, 6).Using re.findall() re.findall() function from the re module is a powerful tool that can be used to match sp
3 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 - Check if String contains any Number
We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should
2 min read
Python - Replace all numbers by K in given String
We need to write a python program to replace all numeric digits (0-9) in a given string with the character 'K', while keeping all non-numeric characters unchanged. For example we are given a string s="hello123world456" we need to replace all numbers by K in the given string so the string becomes "he
2 min read
Python Check If String is Number
In Python, there are different situations where we need to determine whether a given string is valid or not. Given a string, the task is to develop a Python program to check whether the string represents a valid number.Example: Using isdigit() MethodPython# Python code to check if string is numeric
6 min read
Python - Retain Numbers in String
Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for
2 min read