i was writing a code to input a sentence of user then change its whitespaces to ... and print the sentence.
hi rocks I intended to input a sentence and change its whitespaces to "..." I wrote this code:
a=input("inter your sentence: ")
#split in to n str
#print every str with ... as whitespace
a=a.split()
for x in a:
print(x, sep='...',end="")
if I input this is a test, i expected to see
this...is...a...test
but it doesn't work it gives me
"thisisatest
CodePudding user response:
do this.
a=input("inter your sentence: ")
#split in to n str
#print every str with ... as whitespace
a=a.split()
print(*a, sep='...')
Or do this.
a=input("inter your sentence: ")
#split in to n str
#print every str with ... as whitespace
a=a.split()
final_a = '...'.join(a)
print(final_a)
OUTPUT (for input this is a test
)
this...is...a...test
Replying OP's comment. "so what you think about this one for x in range(0,10): print (x ,sep='...', end="") it didn't work too"
for x in range(0,10):
print(x ,sep='...', end="")
Note: sep
seprate args
provide in print method, Default sep=" "
Example:
print("one","1", sep="-->")
# Output: one-->1
And end
: When every args print in terminal then print see the end
value and print it in terminal. Default end="\n"
\n → newline
Example:
for a in range(0, 10):
print(a, end=".|.")
#Output= 0.|.1.|.2.|.3.|.4.|.5.|.6.|.7.|.8.|.9.|.
# Here when 0 prints then print method also print `end`'s value after that, and 1 then `end`'s value and so on.
Answer for your comment's question can be this.
for x in range(0,10):
print(x,end='...')
Output
As I say end
's value print after every print method args print, so '...'
also prints after x
last value(9
).
0...1...2...3...4...5...6...7...8...9...
CodePudding user response:
str.replace
can be used to replace one or multiple characters of same ASCII value to another. In your case, it would be:
a=input("inter your sentence: ")
a=a.replace(' ', '...')
print(a)
CodePudding user response:
You can make use of the Python join and split functions in this case:
The code snippet:
inputSentence = input("Enter your sentence: ")
print("...".join(inputSentence.split()))
The output:
Enter your sentence: This is a test
This...is...a...test