Yami Danchou
Yami Danchou

Reputation: 215

String index out of range, Python

code for a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number the number 1 should be appended to the new string.

The output is correct but it is showing a String index out of range error. Can someone help me on where and how the string index is out of range?

test cases,expected output: (increment_string("foo"), "foo1"),(increment_string("foobar001"), "foobar002"),(increment_string("foobar1"), "foobar2"),(increment_string("foobar00"), "foobar01"),("foobar99"), "foobar100"),("foobar099"), "foobar100"),(increment_string(""), "1")

def increment_string(strng):
   if strng[-1].isdigit():
      exp_strng=strng[::-1]
      new_strng=""
      new_strng1=""
      for i in exp_strng:
         if i.isdigit():
            new_strng+=i
         else:
            break
      new_strng=new_strng[::-1]
      new_strng1=int(new_strng)+1
      new_strng1='{num:{fill}{width}}'.format(num=new_strng1, fill='0', width=len(new_strng))
      return(strng[:-len(new_strng)]+new_strng1)

   else:
      strng+="1"
      return(strng)

Upvotes: 0

Views: 186

Answers (3)

gribvirus74
gribvirus74

Reputation: 765

If think this would be a better solution to your problem:

from re import search

def increment_string(s):
    number = search('\d+$', s)
    if number != None:
        number = number.group()
        first_part = s.split(number)[0]
        return first_part + str(int(number)+1)
    else:
        return s + '1'

I don't know what you want when the number is 9 though: 0 or 10. This code produces 10.

Upvotes: 2

Yami Danchou
Yami Danchou

Reputation: 215

the error was caused when empty string is passed. and I resolved it by adding one more if else:(thanks to Skam)

def increment_string(strng):
if len(strng)>0:
    if strng[-1].isdigit():
        exp_strng=strng[::-1]
        new_strng=""
        new_strng=""
        for i in exp_strng:
            if i.isdigit():
                new_strng+=i
            else:
                break
        new_strng=new_strng[::-1]
        new_strng1=int(new_strng)+1
        new_strng1=str(new_strng1)
        new_strng1=new_strng1.zfill(len(new_strng))
        return(strng[:-len(new_strng)]+new_strng1)
    else:
        strng+="1"
        return(strng)
else:
    strng+="1"
    return(strng)

Upvotes: -1

Jerrybibo
Jerrybibo

Reputation: 1325

Since you gave us more information on the test cases given, you can bypass the edge case of an empty string by modifying the if statement:

def increment_string(strng):
   # Add it here #
   if strng == "":
      return "1"

   elif strng[-1].isdigit():
      exp_strng = strng[::-1]
      new_strng = ""
      new_strng1 = ""
      for i in exp_strng:
         if i.isdigit():
            new_strng += i
         else:
            break
      new_strng = new_strng[::-1]
      new_strng1 = int(new_strng) + 1
      new_strng1 = '{num:{fill}{width}}'.format(num=new_strng1, fill='0', width=len(new_strng))
      return strng[:-len(new_strng)] + new_strng1

   else:
      strng += "1"
      return strng

Upvotes: 2

Related Questions