-3

how can i display a sentence in the my program ..

if i have this sentence: " i play football" and i want to replace a letter "o" for example with "e"....

i do that with the following code and that code Displays the Adjustments that it did on the sentence for example :" feetball" but doesn't Display the sentences for example: "i play feetball" how can i display the complete sentence after editting

the code that i use: ــــــــــــــــــ

f="ذهب الولد إلى الشاطىء و تحدث بشكل هادىء و كان يسيير في بطئ و يمسك في يده شيئ"
x=f.split()

for s in x:
  if s.endswith("ئ") and len(s)==3 :
    print(s.replace("ئ","ء"))
  if s.endswith("ىء") and len(s)>=5:
    print(s.replace("ىء","ئ"))
bgporter
  • 35,114
  • 8
  • 59
  • 65
Ibrahim
  • 9
  • 4

2 Answers2

1

str.replace doesn't change the original string, it returns a new one; strings are immutable in Python. Also, you don't actually change the original list of strings x, so you can't get the complete sentence. Try using enumerate:

for i, s in enumerate(x):
    if s.endswith("ئ") and len(s) == 3:
        x[i] = s.replace("ئ","ء")
    if s.endswith("ىء") and len(s) >= 5:
        x[i] = s.replace("ىء","ئ")

Now the whole sentence is in x:

print(' '.join(x))

This allows you to define multiple replacements that could apply to the same word, but lets you print the whole modified sentence at the end.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
1
for s in x:
  if s.endswith("ئ") and len(s)==3 :
    print(s.replace("ئ","ء"))
  if s.endswith("ىء") and len(s)>=5:
    print(s.replace("ىء","ئ"))

It looks like you're splitting the string into words and performing replacements on each word based on specific criteria. However, if there isn't a replacement defined for a word, you never print it. You need a case for the words where you don't replace any characters:

for s in x:
  if s.endswith("ئ") and len(s)==3 :
    print(s.replace("ئ","ء"))
  elif s.endswith("ىء") and len(s)>=5:
    print(s.replace("ىء","ئ"))
  else:
    print(s)

If you want to put all the output on one line, you can build a list of processed words, re-join them with spaces, and print the result:

l = []
for s in x:
  if s.endswith("ئ") and len(s)==3 :
    l.append(s.replace("ئ","ء"))
  elif s.endswith("ىء") and len(s)>=5:
    l.append(s.replace("ىء","ئ"))
  else:
    l.append(s)
print(' '.join(l))
user2357112
  • 260,549
  • 28
  • 431
  • 505