user11676469
user11676469

Reputation:

Copy Multiple Lines to variable in python using pyperclip

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')

Upvotes: 0

Views: 752

Answers (2)

Florian Bernard
Florian Bernard

Reputation: 2569

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.

Upvotes: 1

Josef Korbel
Josef Korbel

Reputation: 1208

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')

Upvotes: 0

Related Questions