Spaces in file output in Python -
i doing programming in python c background:
developing script automation job. writing data in 1 file below,
f.write("hallo") output : hallo
i want give 4 spaces before writing.
i have tried these options:
f.write(" hallo") f.write(""+""+""+""+"hallo")
but not able achieve want.
with open("demo.txt", "w") f: # open file demo.txt in write mode f.write(" hallo")
don't know trouble getting in way can text file demo.txt written 4 white spaces , hallo
demo.txt
hallo
there many way achieve i.e. let's want print hello
n number of whitespaces ahead of it:
v = " "*n + "hello" # ' hello' (i have used n=4)
another example:
spaces = " "*10 # 10 spaces new_line = "\n"*2 # 2 new lines string = "hello" final = spaces + new_line + string open("demo.txt", "w") f: f.write(final)
note: if reopen existing file in write mode clear content of file , starts writing content
Comments
Post a Comment