Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 different methods to increment numeric strings by K while keeping non-numeric strings unchanged.
Problem Overview
Given a list of strings containing both numeric and non-numeric values, we need to increment only the numeric strings by a given value K ?
Example
Input:
input_list = ["10", "hello", "8", "tutorialspoint", "12", "20"] k = 5
Expected Output:
['15', 'hello', '13', 'tutorialspoint', '17', '25']
Here, 10, 8, 12, and 20 are numeric strings. After adding K=5:
- 10 + 5 = 15
- 8 + 5 = 13
- 12 + 5 = 17
- 20 + 5 = 25
Method 1: Using For Loop and isdigit()
The isdigit() method returns True if all characters in the string are digits ?
# input list of strings
input_list = ["10", "hello", "8", "tutorialspoint", "12", "20"]
print("Input List:", input_list)
k = 5
result_list = []
# traverse through each element
for element in input_list:
if element.isdigit():
# convert to int, add k, then back to string
result_list.append(str(int(element) + k))
else:
# keep non-numeric strings unchanged
result_list.append(element)
print("After incrementing numeric strings by", k, ":")
print(result_list)
Input List: ['10', 'hello', '8', 'tutorialspoint', '12', '20'] After incrementing numeric strings by 5 : ['15', 'hello', '13', 'tutorialspoint', '17', '25']
Method 2: Using List Comprehension
List comprehension provides a concise way to create lists based on existing lists ?
# input list of strings
input_list = ["10", "hello", "8", "tutorialspoint", "12", "20"]
print("Input List:", input_list)
k = 5
# using list comprehension
result_list = [str(int(element) + k) if element.isdigit() else element for element in input_list]
print("After incrementing numeric strings by", k, ":")
print(result_list)
Input List: ['10', 'hello', '8', 'tutorialspoint', '12', '20'] After incrementing numeric strings by 5 : ['15', 'hello', '13', 'tutorialspoint', '17', '25']
Method 3: Without Built-in Functions
This method manually checks if a string is numeric by examining each character ?
def is_numeric(string):
"""Check if string contains only digits"""
digits = "0123456789"
digit_count = 0
for char in string:
if char in digits:
digit_count += 1
return digit_count == len(string)
# input list of strings
input_list = ["10", "hello", "8", "tutorialspoint", "12", "20"]
print("Input List:", input_list)
k = 5
result_list = []
for element in input_list:
if is_numeric(element):
result_list.append(str(int(element) + k))
else:
result_list.append(element)
print("After incrementing numeric strings by", k, ":")
print(result_list)
Input List: ['10', 'hello', '8', 'tutorialspoint', '12', '20'] After incrementing numeric strings by 5 : ['15', 'hello', '13', 'tutorialspoint', '17', '25']
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For Loop with isdigit() | High | Good | Beginners, readable code |
| List Comprehension | Medium | Best | Concise, Pythonic solutions |
| Manual Check | Low | Slowest | Educational purposes |
Conclusion
Use isdigit() with list comprehension for the most Pythonic solution. The manual approach helps understand the underlying logic, while the for loop method offers the best readability for beginners.
