Python字符串与正则表达式练习有哪些技巧?

2026-05-28 19:042阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计327个文字,预计阅读时间需要2分钟。

Python字符串与正则表达式练习有哪些技巧?

1. 判断变位词,如果字符串是另一个字符串的重新排列组合,则这两个字符串互为变位词。例如,heart与earth互为变位词,Mary与arMy也互为变位词。输入示例:x1=input(), x2=input()

1.判断变位词,如果一个字符串是 另一个字符串的重新排列组合,那么这两个字符串互为变位词。比如,”heart”与”earth”互为变位 词,”Mary”与”arMy”也互为变位词

x1=input() x2=input() if sorted(x1)==sorted(x2): print("yes") else: print("no")

2.判断回文串

x=input() x=int(x) for i in range(x): x1=input() x2=x1[::-1] if(x1==x2): print("Yes") else: print("No")

3.统计不同字符个数

x=input() count1=0 count2=0 count3=0 other=0 for i in x: if i.isalpha(): count1+=1 elif i.isspace(): count2+=1 elif i.isdigit(): count3+=1 else: other+=1 print(("%d %d %d %d")%(count1,count2,count3,other))

4.凯撒加密(含汉字)

Python字符串与正则表达式练习有哪些技巧?

s=input() k=int(input()) for p in s: if ord("a")<=ord(p)<=ord("z"): print(chr(ord("a")+(ord(p)-ord("a")+k)%26),end='') elif ord("A")<=ord(p)<=ord("Z"): print(chr(ord("A")+(ord(p)-ord("A")+k)%26),end='') elif u'\u4e00'<=s<=u'\u9fa5': print(chr(ord(p)+k),end='') else: print(p,end = '')

5.数字反转

x=input() str=str(x) if str[0]=='-': str1=-int(str[:0:-1]) else: str1=int(str[::-1]) print(str1)

本文共计327个文字,预计阅读时间需要2分钟。

Python字符串与正则表达式练习有哪些技巧?

1. 判断变位词,如果字符串是另一个字符串的重新排列组合,则这两个字符串互为变位词。例如,heart与earth互为变位词,Mary与arMy也互为变位词。输入示例:x1=input(), x2=input()

1.判断变位词,如果一个字符串是 另一个字符串的重新排列组合,那么这两个字符串互为变位词。比如,”heart”与”earth”互为变位 词,”Mary”与”arMy”也互为变位词

x1=input() x2=input() if sorted(x1)==sorted(x2): print("yes") else: print("no")

2.判断回文串

x=input() x=int(x) for i in range(x): x1=input() x2=x1[::-1] if(x1==x2): print("Yes") else: print("No")

3.统计不同字符个数

x=input() count1=0 count2=0 count3=0 other=0 for i in x: if i.isalpha(): count1+=1 elif i.isspace(): count2+=1 elif i.isdigit(): count3+=1 else: other+=1 print(("%d %d %d %d")%(count1,count2,count3,other))

4.凯撒加密(含汉字)

Python字符串与正则表达式练习有哪些技巧?

s=input() k=int(input()) for p in s: if ord("a")<=ord(p)<=ord("z"): print(chr(ord("a")+(ord(p)-ord("a")+k)%26),end='') elif ord("A")<=ord(p)<=ord("Z"): print(chr(ord("A")+(ord(p)-ord("A")+k)%26),end='') elif u'\u4e00'<=s<=u'\u9fa5': print(chr(ord(p)+k),end='') else: print(p,end = '')

5.数字反转

x=input() str=str(x) if str[0]=='-': str1=-int(str[:0:-1]) else: str1=int(str[::-1]) print(str1)