【每日一练】python字符串

  1. 接受用户输入的字符串,然后反转字符串并输出。
str = input("Enter a string: ")
print(str[::-1])
  1. 接受用户输入的字符串和一个字符,然后统计该字符在字符串中出现的次数。
str = input("Enter a string: ")
character = input("Enter character: ")
print(f"{character}{str}中出现了{str.count(f'{character}')}次")
  1. 接受用户输入的字符串,然后检查该字符串是否是回文(正着读和倒着读都一样)。
str = input("Enter a string: ")
if str == str[::-1]:
    print("该字符串是回文")
else:
    print("该字符串不是回文字符串")
  1. 接受用户输入的字符串,然后将字符串中的大写字母转换为小写字母,小写字母转换为大写字母。
# Get user input
user_input = input("请输入一个字符串: ")
# Swap case and print result
print(user_input.swapcase())
  1. 接受用户输入的字符串,然后移除字符串中的所有空格。
mystr = input("Enter a string:")
print(mystr.replace(" ",""))
  1. 接受用户输入的字符串和一个子字符串,然后检查该字符串是否包含特定的子字符串。
str = input("Enter a string: ")
child_str = input("Enter a child str: ")
if str.find(f"{child_str}") != -1:
    print(f"该字符串{str}存在子字符串{child_str}")
else:
    print(f"该字符串{str}不存在子字符串{child_str}")
  1. 接受用户输入的两个字符串,然后判断它们是否是同一个字符串。
str1 = input("Enter a string: ")
str2 = input("Enter another string: ")
if str1 == str2:
    print("他们是同一字符串。")
else:
    print("他们不是同一字符串。")
  1. 接受用户输入的字符串,然后将字符串中的每个单词的首字母大写。
str1 = input("Enter a string: ")
print(str1.title())
  1. 接受用户输入的字符串,然后计算字符串的长度。
str1 = input("Enter a string: ")
print(len(str1))
  1. 接受用户输入的字符串,然后将字符串中的所有元音字母替换为"*"。
str1 = input("Enter a string: ")
for char in "aeiou":
    str1 = str1.replace(char, '*')
print(str1)
  1. 接受用户输入的两个字符串,然后检查第一个字符串是否以第二个字符串开头。

    str1 = input("Enter a string: ")
    str2 = input("Enter another string: ")
    if str1.startswith(str2) == True:
        print("The first string starts with the second string.")
    else:
        print("The first string does not start with the second string.")
    
  2. 接受用户输入的字符串,然后将字符串中的数字字符删除。

    str1 = input("Enter a string: ")
    for dig in range(10):
        str1 = str1.replace(str(dig), "")
    print(str1)
    
  3. 接受用户输入的字符串,然后统计字符串中单词的数量。

    str1 = input("Enter a string: ")
    res = (str1.split())
    print(len(res))
    
  4. 接受用户输入的字符串,然后检查字符串是否是有效的邮件地址(包含"@"符号)。

    str = input("Enter a string: ")
    if str.count('@') == 1:
        print("Valid email address")
    else:
        print("Invalid email address")
    
  5. 接受用户输入的字符串,然后将字符串按空格分割,然后按字母顺序排序。

    words = input("请输入一个字符串:")
    words = words.split()
    words.sort()
    print("排序后的单词为:")
    print(words)
    
  6. 接受用户输入的字符串,然后统计字符串中每个字符出现的次数。

    words = input("请输入一个字符串:")
    for letter in words:
        res = words.count(letter)
        print(f"{letter}出现了{res}次。")
    
  7. 接受用户输入的字符串,然后查找字符串中的最长单词。

words = input("请输入一个英语句子:")
res = words.split()
longest_word=''
for i in range(len(res)):
    if len(res[i]) >len(longest_word):
        longest_word = res[i]
print(longest_word)
    
  1. 接受用户输入的字符串,然后将字符串中的所有标点符号删除。

    words = input("请输入一个字符串:")
    no_punctuation_text = ''
    for i in words:
        if i.isalnum() or i == " ":
            no_punctuation_text = no_punctuation_text + i
    print(no_punctuation_text)
    

    你的代码实际上已经实现了这个功能,但是它保留了空格。如果你想删除所有的标点符号并保留空格,你可以稍微修改一下你的代码,如下所示:

    words = input("请输入一个字符串:")
    for i in words:
        if i.isalnum() or i.isspace():
            no_punctuation_text = no_punctuation_text + i
    print(no_punctuation_text)
    

    但是,如果你想更精确地控制哪些字符应该被保留,你可以使用Python的string模块中的punctuation字符串,如下所示:

    import string
    
    words = input("请输入一个字符串:")
    for i in words:
        if i not in string.punctuation or i.isspace():
            no_punctuation_text = no_punctuation_text + i
    print(no_punctuation_text)
    

    在这段代码中,我们检查每个字符是否是标点符号,如果是,我们就不打印它。但是,我们仍然打印空格,因为isspace()函数返回True。

  2. 接受用户输入的字符串,然后将字符串中的空格用下划线替换。

    words = input("请输入一个字符串:")
    print(words.replace(" ","_"))
    
  3. 编写一个程序,接受用户输入的字符串,然后判断字符串是否是回文句子(忽略标点符号、空格和大小写)。

    words = input("请输入一个字符串:")
    new_words = ""
    for i in words:
        if i.isalnum():
            new_words = new_words + i
    new_words = new_words.lower()
    if new_words == new_words[::-1]:
        print("是回文句子(忽略标点符号、空格和大小写)。")
    else:
        print("不是回文句子(忽略标点符号、空格和大小写)。")
    
  4. 接受一个字符串作为参数,输出该字符串的字母数、数字数和其他字符数。

    words = input("请输入一个字符串:")
    a = 0
    b = 0
    c = 0
    for i in words:
        if i.isalpha():
            a += 1
        elif i.isdigit():
            b += 1
        else:
            c += 1
    print("字母数:", a, ",数字数:", b, ",其他字符数:", c)
    
  5. 接受两个字符串,判断它们是否为内容相同的单词,如果是,返回 True,否则返回 False。

    word1 = input("请输入一个单词:")
    word2 = input("请输入另一个单词:")
    if sorted(word1.lower()) == sorted(word2.lower()):
        print(True)
    else:
        print(False)
    
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值