部分参考网络,侵删,仅供参考
- 输入年、月(数1-12),日(数1-31)再使用相应的月份名等将日期打印出来。
类似输入:
year:1989
Month(1-12):08
Day(1-31):16
输出:
August 16th,1989
months=['January','February','March','April','May','June','July','August',
'September','October','November','December']
endings=['st','nd','rd']+17*['th']\
+['st','nd','rd']+7*['th']\
+['st']
year =input('Year: ')
month =input('Month(1-12): ')
day =input('Day(1-31): ')
month_number=int(month)
day_number=int(day)
month_name=months[month_number-1]
ordinal=day+endings[day_number-1]
print (month_name+' '+ordinal+','+year)
- 在做下面的每个练习时,都编写一个独立的程序,并将其保存为名称类似于name_cases.py 的文件。
2.1将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello Eric, would you like to learn some Python today?”。
name = 'Eric'
print('Hello '+name +',would you like to learn some Python today?')
- 2调整名字的大小写:将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。
name = 'Eric'
name_sma = name.lower()
name_big = name.upper()
name_name = name.title()
print(name_sma,name_big,name_name)
- 3 名言1:找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
print('Albert Einstein once said, “A person who never made a mistake never tried anything new.”')
- 4名言2:重复练习 2(3),但将名人的姓名存储在变量 famous_person 中,再创建要显示的消息,并将其存储在变量 message 中,然后打印这条消息。
famous_person = 'Albert Einstein'
message = 'A person who never made a mistake never tried anything new.'
print(famous_person+'once said, “'+message+'”')
- 5 剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t"和"\n"各一次。
打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数 lstrip()、
rstrip()和 strip()对人名进行处理,并将结果打印出来。
name = '\t Eric \n '
name1 = name.lstrip()
name2 = name.rstrip()
name3 = name.strip()
print('#'+name+'#')
print('#'+name1+'#')
print('#'+name2+'#')
print('#'+name3+'#')
- 输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
num = input()
l = list(reversed(num))
result = []
for i in l:
if i not in result:
result.append(i)
print("".join(result))
- 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
from tkinter import N
num = int(input())
def change(num):
key_value = {}
for i in range(num):
key,value = input().split(' ')
key = int(key)
value = int(value)
if(key in key_value):
key_value[key]+= value
else:
key_value[key] = value
print('\n')
for x,y in key_value.items():
print(x,y)
change(num)
- 连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
str = input()
def string_out(str):
if len(str) <= 8:
print(str + "0"*(8-len(str)))
else:
while len(str) > 8:
print(str[:8])
str = str[8:]
print(str + "0"*(8-len(str)))
return
string_out(str)
- 输入一个字符串,对字符中的各个英文字符,数字,空格进行统计(可反复调用)
按照统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASII码由小到大排序输出
txt = input()
counts = {}
for t in txt:
if t.isalpha():
counts[t] = counts.get(t,0) + 1
elif t.isnumeric():
counts[t] = counts.get(t,0) + 1
elif t.isspace():
counts[' '] = counts.get(t,0) + 1
ls = list(counts.items())
ls.sort(key=lambda x:x[1],reverse=True)
for i in range(len(ls)):
t,c = ls[i]
print("{0:<10}{1:>5}".format(t,c))
- 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
from tkinter import N
num = int(input())
def change(num):
key_value = {}
for i in range(num):
key,value = input().split(' ')
key = int(key)
value = int(value)
if(key in key_value):
key_value[key]+= value
else:
key_value[key] = value
print('\n')
for x,y in key_value.items():
print(x,y)
change(num)
- 在字符串中找出连续最长的数字字符串。
输入:一个字符串。
输出:字符串中最长的数字字符串和它的长度。如果有相同长度的串,则要一块儿输出,但是长度还是一串的长度
from ast import Num
import sys
from unicodedata import numeric
def solve():
for line in sys.stdin:
str = list(line)
count = 0
length = 0
num = 0
i = 0
temp = []
for i in range(str.__len__()):
if (str[i] >= '0' and str[i] <= '9'):
count += 1
temp.append(str[i])
else:
if count > length:
length = count
count = 0
num = 0
result = []
s = ''.join(temp)
result.append(s)
temp = []
else:
if count == length:
s = ''.join(temp)
result.append(s)
temp = []
count = 0
num = num + 1
else:
temp = []
count = 0
for i in range(len(result)):
print(result[i])
print("%d" % (length))
solve()
- 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
def del_string(string):
dic = {}
for i in string:
if i not in dic.keys():
dic[i] = string.count(i)
count = sorted(dic.values())[0]
for key, value in dic.items():
if value == count:
string = string.replace(key, '')
print(string)
s = input()
del_string(s)
- 输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
tmpStr = input()
alphaNum=0
numbers=0
spaceNum=0
otherNum=0
for i in tmpStr:
if i.isalpha():
alphaNum +=1
elif i.isnumeric():
numbers +=1
elif i.isspace():
spaceNum +=1
else:
otherNum +=1
print('字母=%d'%alphaNum)
print('数字=%d'%numbers)
print('空格=%d'%spaceNum)
print('其他=%d'%otherNum)