Is there any way to replace certain parts in a string of text with different text? Like this:

# This is not a functioning code, the actual meaning is totally different from what I'm trying to depict
text = 'This is a line of text'
print text # shows "This is a line of text"
for all 'is' in text: # <=== fake code, just showing you what I want :3
    replace 'is' with 'is not'
print text # shows "This is not a line of text"

and I also want to know how to make something that changes without making more lines. What I mean is that, say you have a countdown timer. This may be your output:

01:00
00:59
00:58
00:57

... and so on. I want to know how to do something like this:

01:00 <--- This 01:00 changes to 00:59 on the same line

Recommended Answers

All 6 Replies

Is there any way to replace certain parts in a string of text with different text? Like this:

# This is not a functioning code, the actual meaning is totally different from what I'm trying to depict
text = 'This is a line of text'
print text # shows "This is a line of text"
for all 'is' in text: # <=== fake code, just showing you what I want :3
    replace 'is' with 'is not'
print text # shows "This is not a line of text"

and I also want to know how to make something that changes without making more lines. What I mean is that, say you have a countdown timer. This may be your output:

01:00
00:59
00:58
00:57

... and so on. I want to know how to do something like this:

01:00 <--- This 01:00 changes to 00:59 on the same line

If you want to replace text in a terminal, the common unix way is to use curses. The curses module works in any linux distribution. Google says that a patch for windows exists but I never tried this on windows.

However, curses is difficult to master and to maintain. If you want something easy and maintainable, you should use a GUI instead (wxpython, tkinter, etc). In a GUI, it is very simple to change the content of a text or a label widget.

Are you looking something like search and replace stuff. I cant get you.....

One way ...

text = 'This is a line of text'
# notice the added spaces
new_text = text.replace(' is ', ' is not ')

print(text)      # This is a line of text
print(new_text)  # This is not a line of text

So there's no way to do the second code (replacing in same line) on my Mac, you're saying, because I have yet to properly learn GUI editing. Can you give me a link that I can go to and learn proper graphic programming? D:

Oh, thanks, vegaseat. One problem tackled.

So there's no way to do the second code (replacing in same line) on my Mac, you're saying, because I have yet to properly learn GUI editing. Can you give me a link that I can go to and learn proper graphic programming? D:

Oh, thanks, vegaseat. One problem tackled.

This page http://wiki.wxpython.org/AnotherTutorial shows many self-contained examples of wxpython programs. You could start with the statictext example which displays a text and use the SetLabel method to change the text's content.
See here http://wiki.wxpython.org/AnotherTutorial#wx.StaticText .

Use module datetime to do time calculations ...

# use Python module datetime to do time calculations

import datetime as dt

# use strptime(string, format) to form a 'datetime.date' object ...
s = "01:00"
print(s)
time_obj = dt.datetime.strptime(s, "%H:%M")

print(time_obj, type(time_obj))

oneminute = dt.timedelta(minutes=1)

# take off one minute
time_obj = time_obj - oneminute

# use time_obj.strftime(format) to format the string again
s2 = time_obj.strftime("%H:%M")

print(s2)

'''result -->
01:00
(datetime.datetime(1900, 1, 1, 1, 0), <type 'datetime.datetime'>)
00:59
'''

Note:
Python module datetime is a complex but powerful module. Explore it and discover its applications.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.