PTA的Python練習題(六)


從 第3章-8 字符串逆序 開始

1.

n = str(input()) n1=n[::-1] print(n1)

 

2.

不是很好做這道題,自己還是C語言的思維,網上幾乎也找不到什么答案

s = input() idx = s.find("#") s = s[:idx] ss = s[:idx] s_16 = "0123456789ABCDEFabcdef"

for c in s: if c not in s_16: s = s.replace(c, '') flag = 0 for c in ss: if c in s_16: flag = ss.find(c) break

if '-' in ss[:flag]: s = '-' + s if len(s) == 0: num_10 = 0 else: num_10 = int(s, 16) print(num_10)

 

3.

濾掉5個字母,還有空格和感嘆號

a=input() count=0 for i in range(0,len(a)): if(a[i]!='A' and a[i]!='E' and a[i]!='I' and a[i]!='O' and a[i]!='U' and a[i]!='!' and a[i]!=' ' and 'A'<=a[i]<='Z'): count=count+1
print(count)

 

4.

我還在思考字符串怎么排序,發現其實之前做過的sort()函數可以用來無差別排序,一下子就簡單了:

ls =list(input().split()) ls1=sorted(ls) print("After sorted") for i in ls1: print(i)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM