简体   繁体   中英

Textwrap with hyphen

I am trying to wrap a list of strings. Strings are long and I want to break to lines with a limit of 70 characters using Textwrap. This part is easy, the problem is I want to add dash/hyphen at the end of created lines, but cannot figure out how to do it with textwrap

For example if line length is 2 and string is

abcdefgh

I want to obtain this

ab-
cd-
ef-
gh

The command I use is:

aq[i]=textwrap.fill(ap[i], 70)

You could try as below

ap = 'abcdefgh'
textwrap.fill(ap, 2).replace("\n","-\n")

output: 'ab-\ncd-\nef-\ngh'

End of the word will miss out - , so you can add explicitly again only for last word.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM