簡體   English   中英

如何在Python中從字符串中的特定位置(而不是索引)中刪除字符

[英]How to remove characters from a specific place in a string(not by index) in Python

所以我有這個代碼:

def myprogram():
import string
import random
import getpass

inputpasswd = getpass.getpass("Password: ")
passwd = ("I<3StackOverflow")
if passwd == inputpasswd:
    qwerty = input("Type something: ")
    def add_str(lst):
        _letters = ("1","2","3","4","5","6","7","8","9","0","q","w","e","r","t","z","u","i","o","p","a","s","d","f","g","h","j","k","l","y","x","c","v","b","n","m","!","#","$","%","&","/","(",")","=","?","*","+","_","-",";"," ")
        return [''.join(random.sample(set(_letters), 1)) + letter + ''.join(random.sample(set(_letters), 1))for letter in lst]

    print(''.join(add_str(qwerty)))
    input("")
else:
    print("Wrong password")
    input("")

我的問題是:我如何制作一個相反的程序,所以它接受了一堆垃圾字母並將其轉換為有意義的文本?

示例:如果我在此程序中鍵入類似“aaaaaaa”的內容,則會將其轉換為“mapma&”at7ar8a2ga-ka *“

在這個新程序中,我想輸入“mapma&”at7ar8a2ga-ka *“並輸出”aaaaaaa“。

這對你有用嗎?:

s="1a23a45a6"
print(s[1::3]) # aaa

這樣做: initial_str = random_str[1:-1:3] ,其中random_str是帶垃圾的字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM