Python Program to Increment Numeric Strings by K


A numeric string in Python is a string that represents a numerical value using digits, signs, and decimal points, allowing for mathematical operations and data processing.

In this article, we will learn a python program to increment numeric strings by K.

Example

Assume we have taken an input list of strings. We will now increment the numeric strings in an input list by k using the above methods.

Input

inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"]
k = 5

Output

['15', 'hello', '13', 'tutorialspoint', '17', '25']

In the above list, 10, 8, 12 20 are the numeric strings. So, the input K value i.e, 5 is added for all those numeric strings and the result string is printed.

10+5 = 15

8+5 = 13

12+5= 17

20+5 = 25

Hence the resultant list is ['15', 'hello', '13', 'tutorialspoint', '17', '25']

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task

  • Create a variable to store the input list of strings and print the list.

  • Create another variable to store the input k value.

  • Initialize an empty list for storing the resultant list.

  • Use the for loop to traverse through each element of the input list.

  • Use the if conditional statement to check whether the current element is a digit or not with the isdigit() function(If all of the characters are digits, the isdigit() method returns True; otherwise, it returns False)

  • Convert the element to an integer using the int() function and add k to it then convert to string using the str() function for appending it to the resultant list with the append() function(adds the element to the list at the end).

  • Else append that current element to the resultant list directly.

  • Print the resultant list after incrementing numeric strings in the input list by K.

Example 1: Using the for loop and isdigit() function

The following program returns a list after incrementing numeric strings in the input list by K using the for loop and isdigit() function –

# input list of strings
inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"]
# printing input list
print("Input List: ", inputList)
# input k
k = 5
# resultant list
resultantList = []
# traversing through each element of the input list
for e in inputList:
    # checking whether the current element is a digit or not
    if e.isdigit():
        # converting element to integer and adding k to it then converting
        # to string for appending it to the resultant list
        resultantList.append(str(int(e) + k))
    else:
        # else appending that element to the resultant list directly
        resultantList.append(e)
# printing resultant list
print("Incrementing numeric strings in input list by 5:\n", resultantList)

Output

On executing, the above program will generate the following output

Input List:  ['10', 'hello', '8', 'tutorialspoint', '12', '20']
Incrementing numeric strings in input list by 5:
 ['15', 'hello', '13', 'tutorialspoint', '17', '25']

Example 2: Using list comprehension and isdigit() function

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

The following program returns a list after incrementing numeric strings in the input list by K using the list comprehension and isdigit() function

# input list of strings
inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"]
# printing input list
print("Input List: ", inputList)
# input k
k = 5
# Using list comprehension for concise syntax
resultantList = [str(int(e) + k) if e.isdigit() else e for e in inputList]
# printing resultant list
print("Incrementing numeric strings in input list by 5:\n", resultantList)

Output

On executing, the above program will generate the following output –

Input List:  ['10', 'hello', '8', 'tutorialspoint', '12', '20']
Incrementing numeric strings in input list by 5:
 ['15', 'hello', '13', 'tutorialspoint', '17', '25']

Method 3: Without using any built-in functions

The following program returns a list after incrementing numeric strings in the input list by K without using any built-in functions

# creating a function that accepts the list element as an argument
# and checks whether it is a numeric number or not
def checkNumeric(n):
  # initializing all the digits as a string
    digitsStr = "0123456789"
    # storing the count of the number of digits in the given string 
    count = 0
    # traversing through each character of the list element
    for c in n:
        # checking whether the current character is in the digits string
        if c in digitsStr:
            # incrementing the count value by 1
            count += 1
    # checking whether the count is equal to the length of the list element
    if(count == len(n)):
        # returning true if the condition is true
        return True
    # returning False 
    return False
# input list of strings
inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"]
# printing input list
print("Input List: ", inputList)
# input k
k = 5
# storing resultant list after incrementing numeric strings
resultantList = []
# traversing through each element of an input list
for e in inputList:
  # checking whether the current element is numeric or not by
  # calling checkNumeric() function
    if(checkNumeric(e)):
        # converting element to integer and adding k to it then converting
        # to string for appending it to the resultant list
        resultantList.append(str(int(e)+k))
    else:
        # else appending that element to the resultant list directly
        resultantList.append(e)
# printing resultant list
print("Incrementing numeric strings in input list by 5:\n", resultantList)

Output

On executing, the above program will generate the following output

Input List:  ['10', 'hello', '8', 'tutorialspoint', '12', '20']
Incrementing numeric strings in input list by 5:
 ['15', 'hello', '13', 'tutorialspoint', '17', '25']

Conclusion

In this article we have learned 3 different methods to Increment Numeric Strings by K. We learned how to use the isdigit() function to determine whether a character is a digit or not. In order to determine whether the given character is a digit or not, we also learned how to take an optional iterable and check.

Updated on: 17-Aug-2023

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements