0

The code i wrote just create a structure and print it in multiple lines How can create a string to contain all the Lines

import pyperclip
symbol = input('Symbol = ')
width = int(input('Width = '))
height = int(input('Height = '))

while height > 0:
 print(symbol * width)
 height = height - 1

print('\nCopy to Clipboard ?\nY For Yes\nN For No\n')
sel = input('')
if sel == 'Y':
 pyperclip.copy('Here i want to copy the Structure')
elif sel == 'N':
 print('Done')
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143

2 Answers2

1

You can use f-string and addition.

results = ""
while height > 0:
    results += f"{symbol * width}\n"
    height - = 1

print(results)

This should produce the same output as you code, but this time you have a unique string.

Florian Bernard
  • 2,561
  • 1
  • 9
  • 22
  • Thanks it worked But i didn't understand it i will search for unique strings –  Nov 07 '19 at 11:30
  • @alsaibi You can addition two string, two produce a final one. Is what the script do and that replace the `join` method. – Florian Bernard Nov 07 '19 at 11:51
0

You can use list comprehension to build up the strings and then use them all at once

import pyperclip
symbol = input('Symbol = ')
width = int(input('Width = '))
height = int(input('Height = '))

structure = '\n'.join([symbol * width for x in range(height)])

print('\nCopy to Clipboard ?\nY For Yes\nN For No\n')
sel = input('')
if sel == 'Y':
    pyperclip.copy(structure)
elif sel == 'N':
    print('Done')
Josef Korbel
  • 1,168
  • 1
  • 9
  • 32