-2

I have created a program on pycharm which allows the user to input a number so a list of items can be created. The program is as follows:

item = int(input("How many items do you want in your list? "))
stringList = []
for i in range(1, item +1):
    stringList.append(input("Enter String {0}: ".format(i)))
print(stringList)

An example of what could be output shows as follows:

How many items do you want in your list? 3
Enter String 1: apple
Enter String 2: banana
Enter String 3: grape
['apple', 'banana', 'grape']

What I'm wanting to do now is to add the length of each string that has been typed by the user. I am fairly new to python and was wondering how one would add the length so the output would be something like this:

Enter String 1: apple
Enter String 2: banana
Enter String 3: grape
['apple', 'banana', 'grape']
The length of the string 'apple' is 5
The length of the string 'banana' is 6
The length of the string 'grape' is 5
The total length of all strings is 21
miradulo
  • 28,857
  • 6
  • 80
  • 93
  • 2
    What has thou tried? I can confirm, it is possible. – miradulo Apr 11 '16 at 07:47
  • I tried a number of different things. I have been sitting here for the past two hours trying different things however I haven't come close to figuring it out. Brains on overload at the moment. Like I said I am fairly new to the whole programing and everything and I really appreciate the feedback I'm getting. – user2388433 Apr 11 '16 at 08:04
  • If you've tried a number of things then the most relevant thing you could include in your post is the things you have tried => what is not working => why _you think_ your attempt is not working. Rather than just keep repeating that you're new to programming and appreciate help. – miradulo Apr 11 '16 at 08:05
  • Thanks for your feedback, will know for next time – user2388433 Apr 11 '16 at 08:15

3 Answers3

0

Use len(string_var)

This will return the length of string string_var

You can add in your code like:

for i in range(1, item +1):
    stringList.append(input("Enter String {0}: ".format(i)))
print(stringList)

total_str_length = 0

for each_string in stringList:
     print("The length of the string '%s' is %s") %(each_string, len(each_string))
     total_str_length += len(each_string)

print("The total length of all strings is %s") % total_str_length

This will print the each string in stringList formatted as expected output.

Manish Gupta
  • 4,438
  • 18
  • 57
  • 104
0

This is the answer I ended up with after tweaking the feedback given:

item = int(input("How many items do you want in your list? "))
itemnum = []
for i in range(1, item +1):
    itemnum.append(input("Enter String {0}: ".format(i)))
print(itemnum)

totalLength = 0

for eachString in itemnum:
    print("The length of the string",(eachString), "is :", len(eachString))
    totalLength += len(eachString)
print("The total length of all strings is:", totalLength)

The out put was:

How many items do you want in your list? 4
Enter String 1: red
Enter String 2: blue
Enter String 3: green
Enter String 4: yellow
['red', 'blue', 'green', 'yellow']
The length of the string red is : 3
The length of the string blue is : 4
The length of the string green is : 5
The length of the string yellow is : 6
The total length of all strings is: 18
-1

The answer by Manish is easiest to understand, but as a one line solution, you could do this, which adds everything together after you've built the list:

total_length = sum(len(word) for word in stringList)
Peter
  • 3,186
  • 3
  • 26
  • 59