Print list items separated by any character instead of comma (Python)

Viewed 57

I'm trying to divide particular length into sections and print section division_result times in row. The rest is divided by 2 and both placed at the beginning and the end of the list.
For now I have this:

section = 180
length_to_divide: int = 2150
interject = ' X '

division_result = length_to_divide / section
sides = (length_to_divide % section) / 2
whole = str(section).split() * int(division_result)
print(sides, whole, sides)

with the output

85.0 ['180', '180', '180', '180', '180', '180', '180', '180', '180', '180', '180'] 85.0

Is there any way to print list items in row, but instead comma split them by interject = ' X ' in my case, I'd like to have an output like:

85.0 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 85.0
4 Answers

The function join of string is your real answer

m_list = ["1", "2", "3", "4"]
print('x'.join(m_list))

output:

1x2x3x4

in order to append more data you can append it at first to the list like so:

m_list = ["1", "2", "3", "4"]
extra_num = 34
print('x'.join([str(extra_num)]+m_list))

output:

34x1x2x3x4

Another option:

section = 180
length_to_divide: int = 2150
sides = str((length_to_divide % section) / 2)
whole = str(section).split() * int(length_to_divide / section)
s = ' X '.join([sides, *whole, sides])
print(s)

This should solve it. I'm assuming whole is a list always.

section = 180
length_to_divide: int = 2150
interject = ' X '

division_result = length_to_divide / section
sides = (length_to_divide % section) / 2
whole = str(section).split() * int(division_result)
print(sides, *whole, sides, sep=' X ')

Gives Output:

85.0 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 180 X 85.0

section = 180
length_to_divide: int = 2150
interject = ' X '

division_result = length_to_divide / section
sides = (length_to_divide % section) / 2
whole = str(section).split() * int(division_result)
string = ' X '.join([str(x) for x in [sides] + whole + [sides]])
print(string)
Related