English 中文(简体)
我愿在印刷一个可操作的通道后添加一条新线。
原标题:I would like to add a new line after printing an iterable in a for loop

我在玩.。 我把这个词装在一份清单中。 我正在用一纸空文和末端=“,以便全页打印。 经过所有信函的打印后,我要将 cur子移至一个新的线。 是否有这样做的 way? 印刷功能的一部分内容?

hidden_word = list(r.get_random_word().lower())
for letters in hidden_word:
    print(f"{letters} ", end="")

这段文字与“空间”一行。

a b a c a d a b r a

但是,在打印说明之后如何进入下一行?

Just (?

还是有转口或属于印刷功能的一部分?

问题回答

加入<条码> 之后:

for letter in hidden_word:
    print(f"{letter} ", end="")
    # Or more clearly/with less work:
    print(letter, end=" ")
print()

或者避免 lo,让 star子加>在争辩之间缺位:

print(*hidden_word)

或使用<代码>str.join,在纸质和打印单体之间添加空间:

print(   .join(hidden_word))

其中任何一项工作都已完成。 备选案文2是最简明的,但也可能是业绩最差的,但你却重新做I/O,因此,邮联的费用根本毫无意义。

你有几种选择。 “最佳”是主观的;选择你认为最简单的东西。 下面是共同办法:

word = r.get_random_word().lower() # note that strings are iterable without being `list`s

# option 1 (your suggestion is fine, but it adds a trailing space)
for letter in word:
  print(letter, end=" ")
print()

# option 2 (print the last character differently)
for index, character in enumerate(word):
  if index == len(word) - 1:
    print(character)
  else:
    print(character, end=" ")

# option 3 (unpack each character into separate arguments, automatically separated by a single space)
print(*word)

# option 4 (explicitly join the characters with a space between each one)
print(" ".join(word))

备选办法3和4显然是最直接的(最短的),因此更倾向于选择。 可以提出的论点是,一个人比其他人要好,但在多数情况下,差别微不足道。

在用空间打印字后,为了将曲线移至新线,你可以毫无理由地使用印刷(印刷)

hidden_word = list(r.get_random_word().lower())
for letter in hidden_word:

    print(f"{letter} ", end="")

# Move to the new line
print()




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签