By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,822 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,822 IT Pros & Developers. It's quick & easy.

can only concatenate list (not "str") to list

P: 1
My program is supposed to reverse the string inputted, print it then extract the numbers and print the numbers of the reversed string in a list.

Expand|Select|Wrap|Line Numbers
  1. def reverse_str(string):
  2.     revstring =('')
  3.     length=len(string)
  4.     i = length - 1
  5.     while i>=0:
  6.         revstring = revstring + string[i]
  7.         i = i - 1
  8.     return revstring
  9. def strip_digits(string):
  10.     result = []
  11.     for c in string:
  12.         result = result + c
  13. string = raw_input("Enter a string->")
  14. new_str = reverse_str(string)
  15. print new_str
  16. numberless_str  = strip_digits(string)
Oct 16 '12 #1
Share this Question
Share on Google+
2 Replies

Rabbit
Expert Mod 10K+
P: 12,441
Please use code tags when posting code.

You've told us what you want to do. Now what's your question?
Oct 16 '12 #2

Expert 100+
P: 626
can only concatenate list (not "str") to list
You should have included the complete error message which includes the offending line.

"+" concatenates 2 lists. To add another string to an existing list use append (see "list methods"). Note also that you pass "string" to strip_digits(), not "new_str". Finally do not use "string" as a variable name as string is already used by python.
Oct 16 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.