Home » Python » python – Printing each word from one text, following each word of another text-Exceptionshub

python – Printing each word from one text, following each word of another text-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

Trying to do this text mixing program using Python, but i’m a complete noob to programming. Can someone help out on this?

Text 1: Cements used in construction are usually inorganic, often lime or calcium silicate based.

Text 2: No dude, you said sodium chloride. Yes it’s the same as salt, but you could’ve just

Result: Cements (No) used (dude,) in (you) construction (said) ….

How to&Answers:
Text1="Cements used in construction are usually inorganic, often lime or calcium silicate based."
Text2="No dude, you said sodium chloride. Yes it's the same as salt, but you could've just"
a=Text1.split(" ")
b=Text2.split(" ")
c=""
print(a,b)
for i,j in zip(a,b):
    c=c+i+" "+"("+j+") "
print(c)

Answer:

phrase1 = "Cements used..."
phrase2 = "No, dude..."

# split strings into a list of words
words1 = phrase1.split(' ')
words2 = phrase2.split(' ')

# for each pair of words from the same position
# in both phrases, print in the specified format
for word1, word2 in zip(words1, words2):
    print(f'{word1} ({word2})', end=' ')
print()