Search code examples
pythonstringinteger

How to determine the length of a string?


I have a code who transforms a csv table in an different csv table, for example the numbers in the file are shortened and written to a new file. All numbers who are longer than seven digits have to be ignored from the programm.

I tried to fix it, with following code:

if lineSplit[2] > len(7)
    ...
else:
    continue

This leads to an error that a "len" cannot be used on an "int". I also tried to make a string for the lineSplit:

str(lineSplit[2]) 

or a with a variable like that:

a = lineSplit[2]
str(a) > len(7)

But none of this works.


Solution

  • if lineSplit[2] > len(7):
        ...
    else:
        continue
    

    The len() is used with str or any object type other than int, the 7 is already an int that is why it is causing error, remove the len() around 7. Secondly lineSplit[2] is a string, we should get the length of characters if we use len() around it. So try the following code:

    len(lineSplit[2]) > 7