编程实现:由用户从键盘输入一行任意字符串,打印输出该字符串中的总字符数,其中不重复的字符多少个、具体是哪些?
参考如下:
input_str = str(input("请输入任意内容:"))
count = 0
unreplace = []
for i in input_str:
if i not in unreplace:
unreplace.append(i)
count += 1
print("该字符串的中字符数是%d个" % len(input_str))
print("其中不重复的字符有%d个" % count)
print("分别是:", " ".join(unreplace))
效果如下:
该回答引用GPT:
这道题目的主要目的是让程序员熟悉字符串的处理以及如何根据问题需求进行数据处理和筛选。下面是一个简单的Python程序实现代码。
# 由用户从键盘输入一行任意字符串
string = input("请输入一个字符串:")
# 统计字符串中的总字符数
total_character_count = len(string)
# 统计字符串中不重复的字符以及个数
unique_character_set = set(string)
unique_character_count = len(unique_character_set)
# 打印输出结果
print("该字符串中的总字符数为:%d" %total_character_count)
print("该字符串中不重复的字符个数为:%d" %unique_character_count)
print("该字符串中不重复的字符分别为:", end="")
for character in unique_character_set:
print(character, end=" ")
上述代码中,首先通过input函数获取用户输入的字符串,然后使用Python内置的len函数统计字符串中字符的总数。接着,通过Python的set数据类型存储不重复的字符,并使用len函数统计不同字符的个数。最后,使用for循环依次输出不重复的字符。
例如,假设用户输入的字符串为"hello, world!",则总字符数为13,不重复的字符有10个,分别为"h","e","l","o"," ,"w","r","d","!"。
如还有疑问,可留言帮助解决。