Correct Sentence
问题描述:
For the input of your function, you will be given one sentence. You have to return a corrected version, that starts with a capital letter and ends with a period (dot).
Pay attention to the fact that not all of the fixes are necessary. If a sentence already ends with a period (dot), then adding another one will be a mistake.
Input: A string.
Output: A string.
def correct_sentence(text: str) -> str:"""returns a corrected sentence which starts with a capital letterand ends with a dot."""# your code herereturn textif __name__ == '__main__':print("Example:")print(correct_sentence("greetings, friends"))# These "asserts" are used for self-checking and not for an auto-testingassert correct_sentence("greetings, friends") == "Greetings, friends."assert correct_sentence("Greetings, friends") == "Greetings, friends."assert correct_sentence("Greetings, friends.") == "Greetings, friends."assert correct_sentence("hi") == "Hi."assert correct_sentence("welcome to New York") == "Welcome to New York."print("Coding complete? Click 'Check' to earn cool rewards!")
那么就是对于字符串的首字母大写,然后如果末尾不是句号,就加个句号。
试了一下string.capitalize(),发现不行,它会将字符串的第一个字母变成大写,其他字母变小写。
所以只能切片,再转换。用capitalize,upper或者title都可以。
def correct_sentence(text: str) -> str:"""returns a corrected sentence which starts with a capital letterand ends with a dot."""# your code heretext=text[0].upper()+text[1:]if text[-1]=='.':return textelse:text=text+'.'return text
可以再精简一下:
text=text[0].upper()+text[1:]return text if text[-1]=='.' else text+'.'
其他解决方案:可以用join,
return "".join([text[0].upper(), text[1:].strip("."), "."])
Correct Sentence相关推荐
- 【Python CheckiO 题解】Correct Sentence
CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...
- [渝粤教育] 南开大学 思辨式英文写作 参考 资料
教育 -思辨式英文写作-章节资料考试资料-南开大学[] 随堂小测:What are the characteristics of critical essays? 1.[多选题]Which of th ...
- Python CheckiO 题解系列 丨 博客目录索引
CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本题解系列主要记录自己在用 Python 闯关时的做 ...
- Checkio代码闯关小计
首字母大写并增加标点关卡(Correct Sentence): def correct_sentence(text: str) -> str: """ return ...
- 《Python自然语言处理-雅兰·萨纳卡(Jalaj Thanaki)》学习笔记:07 规则式自然语言处理系统
07 规则式自然语言处理系统 7.1 规则式系统 7.2 规则式系统的目的 7.2.1 为何需要规则式系统 7.2.2 使用规则式系统的应用 7.2.3 练习 7.2.4 开发规则式系统需要的资源 7 ...
- 【英语魔法俱乐部——读书笔记】 2 中级句型-复句合句(Complex Sentences、Compound Sentences)...
[英语魔法俱乐部--读书笔记] 2 中级句型-复句&合句(Complex Sentences.Compound Sentences):(2.1)名词从句.(2.2)副词从句.(2.3)关系从句 ...
- java体系结构最下层_JAVA的体系结构中,最下层是(),由适配器和JAVA OS组成,保证JAVA体系结构可以跨平台。...
String s = "To be or not to be"; String ss[] = s.split(" "); System.out.println( ...
- DS||Simply Syntax
题目描述 In the land of Hedonia the official language is Hedonian. A Hedonian professor had noticed that ...
- 数组元素第一个下表java_java中数组的第一个元素下标从__________开始
[单选题]一般生理情况下,每分解1分子ATP,通过钠泵运转可使 [简答题]请创建一个基本的HTML网页,本例的运行效果如图所示. 具体步骤如下: (1)创建一个html文件,将其命名成"du ...
最新文章
- android 将bitmap存为 bmp格式图片大小,Android Bitmap保存為.bmp格式,圖像轉化為黑白圖片...
- 安川g7接线端子图_常用变频器接线端子集锦及接线示意图
- HTTP中常用响应头
- 张首晟:量子计算、人工智能与区块链
- 深入浅出理解 Variable used in lambda expression should be final or effectively final
- 最常见的读入数据方法集锦
- cuda笔记-初始化矩阵及thread,block,grid概念
- linux下/var/run目录下.pid文件的作用(文件锁,防止重复启动)
- SpringBoot 2.1.5(38)---热部署(devtools)配置操作
- linux内核驱动之 驱动程序的角色
- 基于知识图谱的推荐系统总结
- SQL Server统计信息以及如何在SQL中执行更新统计信息
- 微信公众平台开发(77) 图片下载
- vue 项目 upload上传图片 并实现拖拽排序
- 企业纯内网二进制完美部署Docker(20.10.7版本)
- Warmup预热学习率
- day 03 字符串
- 最后一次——时间序列分析
- 四川农业大学计算机考研难度,四川农业大学考研难吗?一般要什么水平才可以进入?...
- (Ryan的Koa系列博客)6.依赖库:on-finished(本文尚未完成)
热门文章
- 处理动态图的图神经网络
- Hydra – Brute Force HTTP(S)
- 媒体邀约尴尬瞬间(二三则)
- 谣言检测文献阅读二—Earlier detection of rumors in online social networks using certainty‑factor‑based convolu
- c语言中英语部分,C语言部分函数(国外英语资料).doc
- Docker网络配置
- macos 13 Ventura beta4(22A5311f) 官方原版dmg镜像
- python能参加奥赛吗-孩子学编程都能参加哪些含金量的比赛?
- 思科、华为交换机err-disable的相关排查解决
- 极客时间-左耳听风-程序员攻略-机器学习和人工智能