Python 3 entry applet

Posted by Alex C on Sun, 02 Jan 2022 18:42:20 +0100

catalogue

1, From beginning to end

Title Requirements:

  1. Enter a three digit integer less than 500 from the keyboard
  2. You need to re-enter when the input does not meet the conditions
  3. Multiply the correct integer by 2 to get a new number
  4. Finally, the new number is flashed from end to end
def study_num():
    flag=0
    while flag==0:
        num=int(input("Please enter a three digit number less than 500:"))
        if 100 <= num < 500:
            num=num*2
            num_str=str(num)  #The slicing operation can output the contents in reverse order, but there is no slicing operation for numeric variables. You can convert them into character types for slicing operation first
            new_num=num_str[::-1]
            print("This new number is flashed from end to end and output as:",new_num)
        else:
            print("The number entered does not meet the conditions: please re-enter:")
            flag=0

#Call function           
study_num()

2, Delete character

Title Requirements:

  1. Enter two integers in two lines. The first integer represents the index start value begin of string_ Index. The second integer represents the length to be deleted.
  2. string from index begin_ Starting with index, the substring with length is deleted.
  3. The rest of the output string after deletion.
def study_delete_chars():
    orgStr="abfghijklmnopqrstuvwxyz"
    begin_index=int(input("Please enter the index of the start location:"))
    length = int(input("Please enter the length to delete:"))
    #Currently, there is no method to delete substrings
    #This can be deleted by slicing the string
    #You can decompose the topic: deleting the first and second paragraphs of the substring and splicing them together is what we need to get
    newStr1=orgStr[0:begin_index]
    newStr2=orgStr[(begin_index+length):]
    newStr=newStr1+newStr2
    print(newStr)

#Call function  
study_delete_chars()

3, Insert character

Title Requirements:

  1. Known string orgStr
  2. Please add another string sub to the specified index position of string orgStr
  3. And output the added newStr
def study_insert_subStr():
    orgStr="abfz"
    sub="I'm Xiao Ming"
    position_index=int(input("Please enter the index of the location to insert:"))
    #Because the string has no method to insert a substring at the specified index position
    #However, we can achieve the topic requirements through the principle that the list can insert elements at its specified position
    #First convert the original string to a list for element insertion, and then convert the new list to a string
    list1=list(orgStr)
    list1.insert(position_index,sub)
    print(list1,type(list1)) #List insertion, result: ['a ',' I'm Xiao Ming ',' B ',' f ',' Z '] < class' list' >
    newStr="".join(list1)
    print(newStr, type(newStr))  # Results: a I'm Xiao Ming bfz < class' STR '>
    
#Call function      
study_insert_subStr()

Python has join() and OS path. join(), two functions. The specific functions are as follows:

  • . join() method: connect an array of strings. Connect the elements in the string, tuple and list with the specified character (separator) to generate a new string;

  • os.path.join() method: return after combining multiple paths;

. join() operates on the list (using ',', '-', ':', '.' and so on as separators respectively)

a=['1','2','3','4','5']

newStr1="".join(a)
print(newStr1)

newStr2=" ".join(a)
print(newStr2)

newStr3="-".join(a)
print(newStr3)

newStr4=":".join(a)
print(newStr4)

newStr5=".".join(a)
print(newStr5)

newStr6="a".join(a)
print(newStr6)

The operation results are as follows:

12345
1 2 3 4 5
1-2-3-4-5
1:2:3:4:5
1.2.3.4.5
1a2a3a4a5

. join() operates on strings (using ',', '-', ':', '.' as separators respectively)

orgStr="hello world"
newStr1="".join(orgStr)
print(newStr1)

newStr2=" ".join(orgStr)
print(newStr2)

newStr3="-".join(orgStr)
print(newStr3)

newStr4=":".join(orgStr)
print(newStr4)

newStr5=".".join(orgStr)
print(newStr5)

newStr6="a".join(orgStr)
print(newStr6)

The operation results are as follows:

hello world
h e l l o   w o r l d
h-e-l-l-o- -w-o-r-l-d
h:e:l:l:o: :w:o:r:l:d
h.e.l.l.o. .w.o.r.l.d
haealalaoa awaoaralad

. join() operates on tuples (using ',', '-', ':', '.' as separators respectively)

tuple1=('1','2','3','4','5')

newStr1="".join(tuple1)
print(newStr1)

newStr2=" ".join(tuple1)
print(newStr2)

newStr3="-".join(tuple1)
print(newStr3)

newStr4=":".join(tuple1)
print(newStr4)

newStr5=".".join(tuple1)
print(newStr5)

newStr6="a".join(tuple1)
print(newStr6)

The operation results are as follows:

12345
1 2 3 4 5
1-2-3-4-5
1:2:3:4:5
1.2.3.4.5
1a2a3a4a5

. join() performs unordered operations on the dictionary (using ',', '-', ':', '. And so on as separators respectively)

dicc={'name1':'a','name2':'b','name3':'c','name4':'d'}

newStr1="".join(dicc)
print(newStr1)

newStr2=" ".join(dicc)
print(newStr2)

newStr3="-".join(dicc)
print(newStr3)

newStr4=":".join(dicc)
print(newStr4)

newStr5=".".join(dicc)
print(newStr5)

newStr6="a".join(dicc)
print(newStr6)

The operation results are as follows:

name1name2name3name4
name1 name2 name3 name4
name1-name2-name3-name4
name1:name2:name3:name4
name1.name2.name3.name4
name1aname2aname3aname4

os.path.join() operates on the directory

import os
newPath=os.path.join("/home/","good/date/","yuki_dir")
print(newPath)

The operation results are as follows:

/home/good/date/yuki_dir

4, Filter sensitive characters and replace

  1. Enter an English text text
  2. Change the sensitive words to * * * (three asterisks)
  3. And output the changed content

Note: sensitive characters include "luck", "hit" and "bitch", others will not be considered temporarily.

def study_grep_char():
    text=" Oh, fuck! I've lost my keys. Oh, shit! I've lost my keys.Oh, bitch! I've lost my keys."
    text=text.replace("fuck","***")
    text=text.replace("shit","***")
    text=text.replace("bitch","***")
    print(text)
#Call function  
#study_grep_char()

Topics: Python