1

How to print each element of my list to a defined length (5), so for example:

myList = ["abcdefgh", "ijklmnop"]

The output I'm looking for is:

abcde
ijklm

I've tried this (printing dates from an excel sheet):

i = 2
x =0
while i < maxR:
    valuesItems =  wb0["C" + str(rowList[i])].value
    itemDate.append(valuesItems)
    print(len(itemDate[x]))
    i = i+1
    x = x+1

But this prints "19" multiple times (the length of each element, not the element itself)

  • 1
    The operation is called "slicing". This answer is in relation to slicing lists, but slicing strings as you're showing works the same way: https://stackoverflow.com/a/509295/6030926 – picobit Nov 01 '22 at 19:31

1 Answers1

4
for element in myList:
    print(element[:5])
jprebys
  • 2,469
  • 1
  • 11
  • 16