-1

The idea would be this:

a = input("insert name: ")
a
print("hi ", a, "!")
print("hello", a, "!")
print("good morning", a, "!")

but I need to know if there's a way to get all that prints in 1 unique thing but wrapping text kinda like this: print("hi ", a, "!" wrap "hello", a, "!" wrap "good morning", a, "!")

Ty in advance

Geza Kerecsenyi
  • 1,127
  • 10
  • 27
Yukon
  • 57
  • 1
  • 9

6 Answers6

2

I can think of two ways:

  • print("hi ", a, "!\n", "hello", a, "!\n", "good morning", a, "!") - using the \n newline control character (see this question for more info).
  • A generator for the same sort of thing, but easier since it inserts the \n for you:
def join(text):
  for i in range(len(text)):
    text[i] = [item if isinstance(item, str) else ''.join(item) for item in text[i]]
  lines = [' '.join(item) for item in text]
  return '\n'.join(lines)

# Usage:
print(join([[["hi ", a, "!"]], ["hello", [a, "!"]], ["good morning", a, "!"]]))

# Equal to:
print("hi " + a + "!")  # See how nested items (e.g [a, "!"] in ["hello", [a, "!"]]) represent simple concatenation (the equivalent of `+` in `print()`)
print("hello", a + "!")  # while sister items (e.g "good morning" and a in ["good morning", a, "!"]) represent joining with space (the equivalent of ',' in `print()`)
print("good morning", a, "!")

Explanation:

  1. Go through and find where to concatenate, and where to delimit, strings.

    1a. for i in range(len(text)):: loop through the array, storing the current array index

    1b. [item if isinstance(item, str) else ''.join(item) for item in text[i]]: Let's break this down. We take the current item - represented by text[i] (since this is inside the for loop, each item looks something like ["hello", [a, "!"]]) - and loop through it. So, the variable item will have values that look either like "hello" or [a, "!"]. Next, we use isinstance to check if the the item is a string. If so, we just leave it. Otherwise, it's an array, so we concatenate all of the values in it together with no delimiter. So, the full example would be: [[["hi ", "dog", "!"]], ["hello", ["dog", "!"]], ["good morning", "dog", "!"]] becomes [["hi dog!"], ["hello", "dog!"], ["good morning", "dog", "!"]]

  2. lines = [' '.join(item) for item in text]: creates an array with each item in text, a nested array, but joined together using . This results in flattening the nested array into a basic 1D array, with each individual array of items replaced by a version separated by spaces (e.g [['hi', 'my', 'friend'], ['how', 'are', 'you']] becomes ['hi my friend', 'how are you'])

  3. '\n'.join(lines) joins together each string in the flat array, using the \n control character (see link above) as a delimiter (e.g ['hi my friend', 'how are you'] becomes 'hi my friend\nhow are you')
Geza Kerecsenyi
  • 1,127
  • 10
  • 27
1

There are many ways to do this in Python. Here is one using the .format(*args, **kwargs) method of the str class:

print("hi, {0}!\nhello, {0}!\ngood morning, {0}!".format(a))

Notice the '\n' character for a new line.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

Can you just print it with one statement:

print(f"hi {}! hello {a}! good morning {a}!")
Tim Mironov
  • 849
  • 8
  • 22
0

You're looking for the newline character, '\n':

print("hi ", a, "!\n", "hello", a, "!\n", "good morning", a, "!")

Of course, you can use all sorts of string formatting, like f-strings, str.format and printf-style formatting.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

By default, print commands performs a new line operation. But you can change that newline to something else:

a = input("insert name: ")
a
print("hi ", a, "!", end=" ")
print("hello", a, "!", end=" ")
print("good morning", a, "!", end="\n")
PaxPrz
  • 1,778
  • 1
  • 13
  • 29
0

We can split the string into chunks in a generator to make a list, which we can join with newlines

n = 5

wrapped = '\n'.join([string[i:i+n] for i in range(0,len(string),n)])

print(wrapped)

EDIT:

Sorry misunderstood what was meant by 'wrap'.

You could do something like print(sentence1, sentence2, … , sep="\n"), using the .format method suggested by others for each sentence.

Ed Ward
  • 2,333
  • 2
  • 10
  • 16