3

The aim of the following program is to convert words in 4 characters from "This" to "T***", I have done the hard part getting that list and len working.

The problem is the program outputs the answer line by line, I wonder if there is anyway that I can store output back to a list and print it out as a whole sentence?

Thanks.

#Define function to translate imported list information
def translate(i):
    if len(i) == 4: #Execute if the length of the text is 4
        translate = i[0] + "***" #Return ***
        return (translate)
    else:
        return (i) #Return original value

#User input sentense for translation
orgSent = input("Pleae enter a sentence:")
orgSent = orgSent.split (" ")

#Print lines
for i in orgSent:
    print(translate(i))
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Kit Yeung
  • 135
  • 2
  • 2
  • 5
  • There are fundamentally two completely different ways to think about the problem: either we want to collect the results (first linked duplicate) and join them together into a single item (second linked duplicate); or we want to do our printing on the same line (third linked duplicate). – Karl Knechtel Jul 01 '22 at 01:04

3 Answers3

3

Use a list comprehension and the join method:

translated = [translate(i) for i in orgSent]
print(' '.join(translated))

List comprehensions basically store the return values of functions in a list, exactly what you want. You could do something like this, for instance:

print([i**2 for i in range(5)])
# [0, 1, 4, 9, 16]

The map function could also be useful - it 'maps' a function to each element of an iterable. In Python 2, it returns a list. However in Python 3 (which I assume you're using) it returns a map object, which is also an iterable that you can pass into the join function.

translated = map(translate, orgSent)

The join method joins each element of the iterable inside the parentheses with the string before the .. For example:

lis = ['Hello', 'World!']
print(' '.join(lis))
# Hello World!

It's not limited to spaces, you could do something crazy like this:

print('foo'.join(lis))
# HellofooWorld!
Volatility
  • 31,232
  • 10
  • 80
  • 89
3

On py 2.x you can add a , after print:

for i in orgSent:
    print translate(i),

If you're on py 3.x, then try:

for i in orgSent:
    print(translate(i),end=" ")

default value of end is a newline(\n), that's why each word gets printed on a new line.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0
sgeorge-mn:tmp sgeorge$ python s
Pleae enter a sentence:"my name is suku john george"
my n*** is s*** j*** george

You just need to print with ,. See last line of below pasted code part.

#Print lines
for i in orgSent:
    print (translate(i)),

For your more understanding:

sgeorge-mn:~ sgeorge$ cat tmp.py 
import sys
print "print without ending comma"
print "print without ending comma | ",
sys.stdout.write("print using sys.stdout.write ")

sgeorge-mn:~ sgeorge$ python tmp.py 
print without ending comma
print without ending comma | print using sys.stdout.write sgeorge-mn:~ sgeorge$
Suku
  • 3,820
  • 1
  • 21
  • 23