Max_char per line st.code?

Summary

I have a snippet of code that takes input from the user and adds single quotes and comma’s around each entry.

Steps to reproduce

Code snippet:

def format(user_text):
    return ", ".join(repr(s) for s in user_text.replace(',', "").split())

user_text = st.text_area('txt')
st.code(format(user_text))

What I would like to do is create a new like after 80 characters or so (if that line isn’t a comma).

I’ve tried this but it’s printing out twice and I’m not sure why.

updated_text = ", ".join(
                    repr(s) for s in user_text.replace(',', "").split())
                for i, letter in enumerate(updated_text):
                    if i % 80 == 0 and i != ',':
                        updated_text += '\n'
                    updated_text += letter
                return updated_text

@FrocketGaming The issue is that you’re setting updated_text to be the original text with commas, and then appending newlines and characters to that. If you instead start with a new empty string, and append to that, it will work

import streamlit as st


def format(user_text):
    updated_text = ", ".join(repr(s) for s in user_text.replace(",", "").split())
    returned_text = ""
    for i, letter in enumerate(updated_text):
        if i % 80 == 0 and i != ",":
            returned_text += "\n"
        returned_text += letter
    return returned_text


user_text = st.text_area("txt")
st.code(format(user_text))

Ah, so close :smiley:

Thanks for the help.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.