Skip to content
Related Articles

Related Articles

Python – Convert Delimiter separated list to Number

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Jul, 2022

Given a String with delimiter separated numbers, concatenate to form integer after removing delimiter.

Input : test_str = “1@6@7@8”, delim = ‘@’ 
Output : 1678 
Explanation : Joined elements after removing delim “@”
Input : test_str = “1!6!7!8”, delim = ‘!’ 
Output : 1678 
Explanation : Joined elements after removing delim “!” 
 

Method #1 : Using loop + split() + int()

This is one of the ways in which this task can be performed. In this, we split the string on delimiter and then run a loop to concat, at end result is converted to int().

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
# Using loop + split() + join()
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
 
# spliting to get list of string numbers
temp = test_str.split(delim)
res = ''
for ele in temp:
    res = res + ele
 
# converting result into integer
res = int(res)
 
# printing result
print("Constructed integer : " + str(res))

Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

Method #2 : Using join() + split() + int()

This is one another way in which this task can be performed. In this, we perform final concatenation using join() and int() to get final result.

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
# Using join() + split() + int()
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
 
# join used over splitted result
# final result casted using int()
res = int("".join(test_str.split(delim)))
 
# printing result
print("Constructed integer : " + str(res))

Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

Method #3: Using replace() method.

Replacing the delimiter in string with empty string

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
 
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
 
res = test_str.replace(delim,"")
 
# printing result
print("Constructed integer : " + str(res))

Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!