Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用另一个字符替换尾随字符_Python - Fatal编程技术网

Python 用另一个字符替换尾随字符

Python 用另一个字符替换尾随字符,python,Python,我有一些以8结尾的字符串。示例:GEWTIMRZGYYTANJYG488888 我想用等号替换这些8,但结尾的8的数量并不总是一致的,字符串中可能还有其他8。我试过rstrip和replace,但我想不出一个解决方案。没有正则表达式的解决方案 data = "GEWTIMRZGYYTANJYG4888888" stripped = data.rstrip("8") new_data = stripped + "=" * (len(data) - len(stripped)) 无正则表达式的解

我有一些以8结尾的字符串。示例:GEWTIMRZGYYTANJYG488888

我想用等号替换这些8,但结尾的8的数量并不总是一致的,字符串中可能还有其他8。我试过rstrip和replace,但我想不出一个解决方案。

没有正则表达式的解决方案

data = "GEWTIMRZGYYTANJYG4888888"
stripped = data.rstrip("8")
new_data = stripped + "=" * (len(data) - len(stripped))
无正则表达式的解

data = "GEWTIMRZGYYTANJYG4888888"
stripped = data.rstrip("8")
new_data = stripped + "=" * (len(data) - len(stripped))

使用rstrip,然后比较前后字符串的长度,以确定删除了多少个8。然后添加许多EqualUse rstrip,然后比较前后字符串的长度,以确定删除了多少个8。然后加上那么多相等的数