### Thread: Help with strings in lists

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
1
Rep Power
0

#### Help with strings in lists

Hi everyone,

I am pretty new to Python and I am currently taking a course on intro to computer programming. We currently have an assignment on strings and lists. I'm currently out doing field work, so I haven't been able to talk to my professors and I figured I'd come here for some help!

So here is the problem, I am given a list of names

names = ['Bob', 'Susan', 'Mike']

With this list, I create a function that has two variables; one variable is the list, and the other variable is an integer. The integer represents the length of each string in the list. The return value is the number of names in the list that are greater than the integer. For example:

>>> my_function(list, integer)
>>> my_function(names, 4)
>>> 1 (this would be Susan)

So the code I wrote for this goes like this:

def my_function(list, length):

count = 0

for i in range(len(list)):
if len(list[i]) > length:
count = count + 1
return count

So this code seems to work properly, however, there is a second part to this problem that I need to solve.

When I return my number, the function must also update the original list to include empty spaces in the string equal to the length that I specified in my function. For example:

>>> names = ['Bob', 'Susan', 'Mike']
>>> my_function(list, integer)
>>> my_function(names, 4)
>>> 1
>>> names = ['Bob ', 'Susan', 'Mike']

or if

>>> names = ['Bob', 'Susan', 'Mike', '']
>>> my_function(list, integer)
>>> my_function(names, 10)
>>> 0
>>> names = ['Bob ', 'Susan ', 'Mike ', ' '] (for some reason the spaces are not showing up in the thread, each string now has a length of 10)

What I am having trouble with is writing the second part of the function that adds spaces to the end of each string in the list and then updates accordingly. Can anyone help me out with this? I have no idea where to even start with this.

Please remember that I am still really new to this!!! Thanks a lot!!
2. Follow instructions at link in my signature to preserve forum spacing. ljust could work. Or you could use
name+(' '*max(0,length-len(name)))

Code:
```>>> help(''.ljust)
Help on built-in function ljust:

ljust(...)
S.ljust(width[, fillchar]) -> string

Return S left-justified in a string of length width. Padding is
done using the specified fill character (default is a space).```

Here's a short version of the function you showed:
Code:
```def my_function(list, length):
return sum(length < len(OBJECT) for OBJECT in list)```
3. Use code tags ...

[code ]