Python Forum
print function output wrong with strings.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print function output wrong with strings.
#1
Hi Team,
I am beginner in Python and below is my code. I want to print some content in specific format with below code but it is not printed as mention. it will be helpful if someone help to resolve it.

f = open(r"C:\Users\emanpos\bbuser.txt","r")
for x in f:
print("<Number>")
print("      %s%s%s" %("<Number>", x,"</Number>"))
print('</Number>')
Expected Output:
Output:
<Number> <Number>abc</Number> </Number>
But Output comes as below.
Output:
<Number> <Number>abc </Number> </Number>
snippsat write Feb-11-2021, 03:31 PM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
You iterate over lines in file. What makes line a line? Newline at the end (\n)!

It also advisable to open files using with and use f-strings.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
As mention use f-string and with open().
Then it look like this.
with open(r"num.txt", "r") as f:
    print("<Number>")
    for line in f:
        print(f'{" ":<4}<Number>{line.strip()}</Number>')
    print('</Number>'
Output:
<Number> <Number>1</Number> <Number>2</Number> <Number>3</Number> <Number>4</Number> <Number>5</Number> </Number>
Reply
#4
Indentation in Python does the job of {} in C. It is how you block code.

When I try to run your program I get an error.
f = open(r"C:\Users\emanpos\bbuser.txt","r")
for x in f:
print("<Number>")
print("      %s%s%s" %("<Number>", x,"</Number>"))
print('</Number>')
Error:
File "..., line 3 print("<Number>") ^ IndentationError: expected an indented block
I don't know why you aren't getting the same error. But as snippsat says, your program is not going to work the way you want because your code blocks are not properly indented
f = open(r"C:\Users\emanpos\bbuser.txt","r")
for x in f:
    print("<Number>")
    print("      %s%s%s" %("<Number>", x,"</Number>"))
    print('</Number>')
I don't care if you use f'strings or a context manager.
mposwal likes this post
Reply
#5
for example

test.txt content

1
2
3
4
5
6
7

if you want it in lines between <Number> and </Number>

f = open("test.txt", "r").read().splitlines()
for x in f:
    print(f"<Number>{x}</Number>")
Output:
<Number>1</Number> <Number>2</Number> <Number>3</Number> <Number>4</Number> <Number>5</Number> <Number>6</Number> <Number>7</Number>
Reply
#6
Hi,

(Of course you need proper indentation, and f strings are easier)
But if I take your original code and run it in IDLE, (and replace the file with a list of numbers)
it outputs exactly what you would like it to do.

So I suppose the file contains a CRLF on each line, that you should strip first.
Paul
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output correction using print() function afefDXCTN 3 3,553 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 284 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  python prints none in function output chairmanme0wme0w 3 394 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  For loops returning wrong output Nighthound21 1 617 May-03-2021, 03:43 AM
Last Post: deanhystad
  Print output not working xninhox 7 1,406 Jan-16-2021, 09:42 AM
Last Post: xninhox
  Output with none, print(x) in function Vidar567 3 723 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  How to append to list a function output? rama27 5 1,569 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  print function help percentage and slash (multiple variables) leodavinci1990 3 912 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Invalid syntax on print function DoctorSmiles 2 1,310 Jul-12-2020, 07:39 PM
Last Post: DoctorSmiles
  Print output in single file using pramika loop deepakkhw 1 886 Jul-11-2020, 11:57 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

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