Python Forum
How work with formatted text in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How work with formatted text in Python?
#1
Python 3.7.2

I have a specially formatted text, that I just want to post to another site:
my_text = '\
var index_id = 0;\
var len = arr_id.length;\
while(index_id < len){\
    current_id = arr_id[index_id];\
    index_id = index_id + 1;\
    \
    offset = 0;\
    if (index_query == 0){\
        offset = initial_offset;\
    }\
}'
But if I form it as shown above, then it is a single string, that cann't be broken into substrings. And the code abounding with backslashes becomes very ugly.

Of course, I can save this text to a file and read it line by line, but I don't want to introduce unnecessary entities.

How to simply work with formatted text in Python?
Reply
#2
use triple quotes
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
import textwrap


string_for_server = textwrap.dedent("""
    Your text
    In a Block
        4 spaces
        4 spaces
    """).lstrip()
Output:
Help on function dedent in module textwrap: dedent(text) Remove any common leading whitespace from every line in `text`. This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form. Note that tabs and spaces are both treated as whitespace, but they are not equal: the lines " hello" and "\thello" are considered to have no common leading whitespace. (This behaviour is new in Python 2.5; older versions of this module incorrectly expanded tabs before searching for common leading whitespace.)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
buran and DeaD_EyE, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatted string not translated by gettext YvanM 10 1,994 Sep-02-2022, 08:46 PM
Last Post: YvanM
  Getting a GET request output text into a variable to work with it. LeoT 2 2,987 Feb-24-2021, 02:05 PM
Last Post: LeoT
  get docstring to work with another block of text at the top Skaperen 4 2,443 Dec-28-2019, 10:37 PM
Last Post: Skaperen
  Python convert multi line into single line formatted string karthidec 2 9,409 Dec-23-2019, 12:46 PM
Last Post: karthidec
  How to run a method on an argument in a formatted string Exsul 1 1,677 Aug-30-2019, 01:57 AM
Last Post: Exsul
  Want a list utf8 formatted but bytestrings found nikos 28 11,430 Feb-18-2019, 08:26 AM
Last Post: nikos
  Strip does not work when applied on a string read from a text file. susmis666 1 2,386 Dec-27-2018, 07:07 AM
Last Post: perfringo
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 6 24,051 Jun-19-2018, 03:43 PM
Last Post: JohnJSal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020