简体   繁体   English

如何在 Python 中删除字符串的一部分

[英]How do I remove a part of a string in Python

I am coding a calculator.我正在编写一个计算器。 I decided to use 2 strings, one that is shown to the user on a screen, and one, that is for making the calculation.我决定使用 2 个字符串,一个在屏幕上显示给用户,另一个用于进行计算。 I now want to make a function that deletes everything on the screen.我现在想做一个删除屏幕上所有内容的函数。 But now I have the problem, that the variable, that is used to perform the calculation, still has the value of the number inside of it.但是现在我遇到了问题,即用于执行计算的变量仍然具有其中的数字值。 I wanted to ask, how I could remove that number.我想问一下,我怎样才能删除那个号码。 I also use a tkinter window, to show everything.我还使用 tkinter 窗口来显示所有内容。

Here is the function, where I wanted to do that:这是我想做的功能:

def clear_S(event):
    global Evaluation_T    #This gets shown
    global Evaluation_C    #This is for the calculation
    Evaluation_T = "                "
    renew_Label()          #Displays it to a 
    Evaluation_T = ""
    #Logic, to remove that from Evaluation_C

If you need any more information, please ask.如果您需要更多信息,请询问。

It's not how input and output work.这不是输入和输出的工作方式。 When you print something, you print the value of that variable on STDOUT which is a stream file to connect your software with your terminal.当您打印某些内容时,您会在 STDOUT 上打印该变量的值,STDOUT 是将您的软件与终端连接的流文件。 What you're doing it like do a photocopy and edit the original copy expecting that the photocopy will be edited too.您正在做的事情就像复印和编辑原始副本一样,希望复印件也能被编辑。 Have a look at Is there a way to clear your printed text in python?看看有没有办法在 python 中清除打印的文本? it should be what you were looking for.它应该是你要找的。

EDIT: I misunderstand what you asked.编辑:我误解了你的要求。 There are different solutions: 1) If Evaluation_Text is always the end of Evaluation_C you can do:有不同的解决方案: 1)如果 Evaluation_Text 始终是 Evaluation_C 的结尾,您可以执行以下操作:

Evaluation_C = Evaluation_C[:-len(Evaluation_Text)]

or或者

2) You can use replace with reverse string and do it only one time with: 2)您可以使用反向字符串替换,并且只使用一次:

Evaluation_C[::-1].replace(Evaluation_Text[::-1], 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM