how do I print a list of variables and strings into columns in python

Viewed 35

I'm new to python and trying to build a program that involves a long and tedious list of variables that = strings that I would like to optimize but not sure how, and I am trying to print the variables (which output their corresponding strings) into neat columns instead of the long vertical line of outputs that I currently have.

While there are a few thoroughly answered questions about printing strings into columns, I can't find an answer (correct me if I'm wrong) that that will work with variables and strings instead of just the strings.

an example:

myStr_1 = 'neé'
myStr_2 = 'ümlaut'
myStr_3 = 'skårsgard'
myStr_4 = 'scheiß'
myStr_5 = 'kødd'
# and so on

codec_1 = myStr_1.encode('ascii', error=namereplace)
codec_2 = myStr_2.encode('ascii', error=namereplace)
codec_3 = myStr_3.encode('ascii', error=namereplace)
codec_4 = myStr_4.encode('ascii', error=namereplace)
codec_5 = myStr_5.encode('ascii', error=namereplace)
cap_1 = codec_1.upper()
cap_2 = codec_2.upper()
cap_3 = codec_3.upper()
cap_4 = codec_4.upper()
cap_5 = codec_5.upper()

print("first column: ", cap_1)
print("first column: ", cap_2)
print("second column: ", cap_3)
print("second column: ", cap_4)
print("third ?: ", cap_5)

Could I define a function to do all this, encode and do other stuff to each variable together or all at once instead of writing it out one by one like I have?

One way I tried to format it from an answer I found was like this:

str1 = 'one'
str2 = 'two'
str3 = 'three'
str4 = 'four'
str5 = 'five'
str6 = 'six'
str7 = 'seven'


table_data = [
    [str1, str2, str3]
    [str4, str5, str6]
    [str7, 'eight', '9']
]

for row in table_data:
    print("{: >20} {:>20} {:>20}".format(*row))

but it gave me this error: TypeError: list indices must be integers or slices, not tuple

So I'm not sure how to format the variables and print their strings together, and also how to optimize the multiple variables that I have, such that I can enter any input and have the variable effect each input at once instead of tediously copying each variable for each input like in my example. Or in other words, where I have these variables, which I can input n strings into and for example encode each one simultaneously, instead of typing out each string and encode each one separately with a new variable. Hope that makes sense

0 Answers
Related