Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:43:44+00:00 2026-06-16T17:43:44+00:00

I’m writing a program to add two numbers from a single line of input

  • 0

I’m writing a program to add two numbers from a single line of input of the form:

number + othernumber

I keep getting a “string indices must be integers” error, but when I call type on all the indices, they all show up as integers.

How do I fix this?
Here’s the code

S = input()
for position in range(0, len(S)):
   if '+'== position:
     break
a=int(position)
Sum = (S[0,a])+(S[a, len(S)])
print(Sum)
#print(position)   
#print(type(position))
#print(type(len(S)))
#print(type(0)) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:43:46+00:00Added an answer on June 16, 2026 at 5:43 pm

    The immediate issue

    You probably meant to use S[0:a] and S[a:len(S)] (slicing) rather than commas.

    A note about slicing…

    You don’t have to specify the leading zero or the trailing len(S) there – they’re implicit. So you could just use S[:a] and S[a:] to mean the same thing.

    Also note that S[0:a] + S[a:len(S)] is equivalent to S. You probably didn’t want to include the + in there, so you’d probably want to use S[a+1:len(S)] instead.

    Another note about finding the position of a character in a string

    You don’t need to loop over the indices manually – there’s already the .index() method of strings to do this:

    >>> "hello".index("e")
    1
    

    A simpler way to accomplish your overall goal

    You can just use the split() function to get the parts of a string separated by the + character:

    S = input()
    number_strings = S.split('+')
    numbers = [int(n) for n in number_strings]
    print sum(numbers)
    

    As a bonus, this will work for an arbitrary number of numbers – 1+2+3 would work, as would just 4.

    The third line uses what’s called a list comprehension to operate on each of the elements of a list and generate a new one – in this case, taking a list of strings and making a list of integers.

    The fourth line takes advantage of Python’s build in sum() function, which will automatically return the sum of a sequence of items.

    Note that you could also condense the above lines:

    print sum(int(n) for n in input().split('+'))
    

    This is a much tidier form; I just spaced it out above to make it easier to explain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.