Home > Enterprise >  How would I limit the number of characters per line in python from an input
How would I limit the number of characters per line in python from an input

Time:04-11

Would there be a way to limit the amount of characters that are printed per line?

  while 1:
    user_message = ""
    
    messageQ = input("""\nDo you want to enter a message?
  [1] Yes    
  [2] No

  [>] Select an option: """)
    if messageQ == "1":
      message = True
    elif messageQ == "2":
      message = False
    else:
      continue

    if message == True:
        print(
"""
-----------------------------------------------------------------

You can enter a custom message that is below 50 characters.
""")
    custom_message = input("""\nPlease enter your custom message:\n \n> """)
    if len(custom_message) > 50:
        print("[!] Only 50 characters allowed")
        continue
    
    else:
        print(f"""
Your Custom message is:
{custom_message}""") #here is where I need to limit the number of characters per line to 25
    
    
    break

So where I print it here:

Your Custom message is:
{custom_message}""") #here is where I need to limit the number of characters per line to 25

I need to limit the output to 25 characters per line.

CodePudding user response:

You might use textwrap.fill to break excessively long string into lines, example usage

import textwrap
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
print(textwrap.fill(message, 25))

output

Lorem ipsum dolor sit
amet, consectetur
adipiscing elit, sed do
eiusmod tempor incididunt
ut labore et dolore magna
aliqua. Ut enim ad minim
veniam, quis nostrud
exercitation ullamco
laboris nisi ut aliquip
ex ea commodo consequat.
Duis aute irure dolor in
reprehenderit in
voluptate velit esse
cillum dolore eu fugiat
nulla pariatur. Excepteur
sint occaecat cupidatat
non proident, sunt in
culpa qui officia
deserunt mollit anim id
est laborum.

CodePudding user response:

You can do

message = "More than 25 characters in this message!"
print(f"{message:.25}")

Output

More than 25 characters i

CodePudding user response:

>>> my_str = """This is a really long message that is longer than 25 characters"""
#For 25 characters TOTAL
>>> print(f"This is your custom message: {my_str}"[:25])
'This is your custom messa'
#For 25 characters in custom message
>>> print(f"This is your custom message: {my_str[:25]}")
This is your custom message: This is a really long mes

This takes advantage of the substring operator. This cuts off any characters past the 25th character.

CodePudding user response:

As have already checked that the message is not more than 50 characters we just need to know whether it is more or less than 25 characters long.

ln = len(custom_message) -1 # because strings are 0 indexed
if ln  < 25:
  print(custom_message) 
else:
  print(f"This is your custom message: {my_str}"[:ln])
  print(f"This is your custom message: {my_str}"[25:ln])
``
  • Related