Home > Software design >  doubling object in Python
doubling object in Python

Time:12-04

how do I double the object side by side?

print("    *\n   * *\n  *   *\n *     *\n***   ***\n  *   *\n  *   *\n  *****" *2)

this code puts the objects one below another, how do i do that it prints it besides?

CodePudding user response:

You could try something like that:

obj = "    *\n   * *\n  *   *\n *     *\n***   ***\n  *   *\n  *   *\n  *****"

lines = obj.split('\n')
space = len(max(lines, key=len))   3

for line in lines:
    print(line   " "* (space - len(line))   line)

CodePudding user response:

Use a multiline string for your image, iterate through the lines and print each one doubled:

# using dots to indicate spaces clearly
s = '''\
....*.....
...*.*....
..*...*...
.*.....*..
***...***.
..*...*...
..*...*...
..*****...
'''.replace('.',' ')

# double each line
for line in s.splitlines():
    print(line * 2)

Output:

    *         *     
   * *       * *    
  *   *     *   *   
 *     *   *     *  
***   *** ***   *** 
  *   *     *   *   
  *   *     *   *   
  *****     *****   

CodePudding user response:

This works:

arrow = "    *\n   * *\n  *   *\n *     *\n***   ***\n  *   *\n  *   *\n  *****"
    
arrow_lines = arrow.split("\n")
double_arrow_lines = [line  " "*(15-len(line))  line for line in arrow_lines]
double_arrow = "\n".join(double_arrow_lines)
    
print(double_arrow)

CodePudding user response:

To print the object side by side, you can use the join() method. The join() method takes a list of strings and concatenates them together, using the string on which the method is called as a separator.

For example, you can use the following code to print the object side by side:

obj = " *\n * \n * \n * \n ***\n * *\n * *\n *****"

print("\n".join([obj, obj]))

This will print the object twice, with a newline between each instance.

Alternatively, you could use string multiplication to repeat the string, and then use the print() function to print the result on a single line, like this:

obj = " *\n * \n * \n * \n ***\n * *\n * *\n *****"

print(obj * 2, sep="")

In this case, we use the sep parameter of the print() function to specify that no separator should be used between the items being printed. This will print the objects side by side on a single line.

  • Related