0

I have a string like this:

location = "IP.Location.1"

and there are other locations like IP.Location.2, IP.Location.3, etc.

How can I increment the location from IP.Location.2 to IP.Location.3? I always need to increment the numerical part by 1.

4
  • Did you try splitting your string by "."? Then you can concatenate the first two entries in your split (with "." inbetween) and additionally to a str() of your number that you increment Commented Apr 21, 2021 at 13:04
  • Strings are immutable in python. But there are ways around it so that you can modify a string. One way would be to make the string a list and then replace the last item with the incremental number Commented Apr 21, 2021 at 13:05
  • 1
    @MEKH do you want to take a string like "IP.Location.1" as input and create the corresponding string "IP.Location.2" as output, or do you want to take a number like 5 as input and create the corresponding string "IP.Location.5" as output? Commented Apr 21, 2021 at 13:08
  • Doesn't matter, the answers explain almost every combo. Commented Apr 21, 2021 at 13:14

4 Answers 4

2

Several ways to achieve this but this would be an easy way if you are pre Python 3.6:

for i in range(1, 11):
    print('IP.Location.{my_number}'.format(my_number=i))

If you have Python 3.6+ then:

for i in range(1, 11):
    print(f'IP.Location.{i}')

Finally if you just have the string and you want to increment up from it then extract the int from the string, extract just the non-int bit and use that as your string and range:

location = "IP.Location.1"
initial_number = int(''.join(filter(str.isdigit, location)))
string_phrase = ''.join([i for i in location if not i.isdigit()])

for i in range(initial_number, initial_number + 10):
    print(f'{string_phrase}{i}')
Sign up to request clarification or add additional context in comments.

2 Comments

That last piece of code won’t work if <IP> is not the literal string “IP”, but an actual IP number (dotted quad).
There are a lot of unknowns with an ambiguous question. Between the various answers the OP has likely got a solution which is the goal.
1

Here is a Python 3.6 + solution:

for i in range(10):
    print(f'IP.Location.{i + 1}')

Comments

0

Here’s a generic solution that does exactly what you asked for, i.e. incrementing an integer number at the end of a string that is separated by a dot.

location = "IP.Location.1"

parts = location.rsplit(".", 1)
parts[1] = str(int(parts[1]) + 1)
location = ".".join(parts)

print (location)
# --> IP.location.2

6 Comments

what about increment the location which is outside the string
I’m afraid I don’t get what you mean.
the value that saved the string
I’m very sorry, but I still don’t understand. Maybe give an example?
I want to save each value in location1 = "IP.Location.1" location2 = "IP.Location.2"
|
0

Have you tried adding the strings? See code below.

# create base string
location = "IP.Location."
# introduce counting variable
count = 1
# create empty list to store results
ip_locs = []

# loop to get incremented strings
for i in range(10):
    ip_loc = location + str(count)
    ip_locs.append(ip_loc)
    count = count+1

print(ip_locs)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.