-1

Remember: The , character after our print statement means that our next print statement keeps printing on the same line. Let's filter out the letter 'A' from our string.

phrase = "A bird in the hand..."

Do the following for each character in the phrase.

  • If char is an 'A' or char is an 'a', print 'X', instead of char. Make sure to include the trailing comma.
  • Otherwise (else:), please print char, with the trailing comma.

For the above question, I got the below code:

phrase = "A bird in the hand..."

for char in phrase:
    if char == 'A' or char =='a':
        print('X')
    else:
        print (char)

The output:

X
 
b
i
r
d
 
i
n
 
t
h
e
 
h
X
n
d
.
.
.

How do I get the output on one line?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Deepak47
  • 9
  • 1
  • 3
  • The hint about a trailing comma strongly suggests that you are using Python 2 (and aren't very good at reading instructions). You really need to clarify this; unless you specifically indicate otherwise, anyone who hasn't been hiding under a rock for the last couple of years will assume you are asking for help with the currently supported and recommended language, Python 3. – tripleee Aug 06 '20 at 12:30

3 Answers3

1
print(value, end="")

BTW, for your task, you can just:

processed = phrase.replace('A', 'x').replace('a', 'x')
print(processed)
Or Y
  • 2,088
  • 3
  • 16
1

Whenever you need an output printed in the same line, all you need to do is to terminate your print function with the argument 'end' in Python3

phrase = "A bird in the hand..."
for char in phrase.lower(): 
    if char =='a': 
        print('X', end="" )
    else: print (char, end="" )
Bijin Abraham
  • 1,709
  • 2
  • 11
  • 25
0

Another way to do this is to concatenate each part of the string together and print it all out at once.

finaloutput = “”
phrase = “A bird in the hand...”
for char in phrase.lower():
    if char == “a”:
        finaloutput += X
    else:
        finaloutput += char

print(finaloutput)
Andrew C
  • 137
  • 2
  • 10