-5

I am trying to make sure that no matter the length of the elements on a list I have, only the first X characters are displayed when I do an output.

For instance if I had:

DATA = 
Column1 Column2 Column3
ABCD    1234    5678
FGHI    0987    6543
ABCDEFGH135791011   3545455

I would like that last line to only select the first 7 characters so I can print:

Column1 Column2 Column3 
ABCD    1234    5678 
FGHI    0987    6543 
ABCDEFG 1357910 3545455

I will miss some text and number but will have the main data in a readeable format.

Also, this is not about "How to do a substring". I know that if A = 'abcdefg' I can simply do A[:5] to get the first 5 characters. What I want is to do that on a list without having a nested "for". (yeah one could simply build a FOR each IN ... and create a new table with each elemented being trimmed). I am interested to know if there is an elegant "Pythonic" way of doing that.

Thanks!

Yona
  • 571
  • 7
  • 23

1 Answers1

0
>>> s = "ABCDEFGH135791011"
>>> print s[:7]
ABCDEFG
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118