栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

标题中的字符串带有例外

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

标题中的字符串带有例外

这有一些问题。如果使用拆分和合并,则某些空格字符将被忽略。内置的大写和标题方法不会忽略空格。

>>> 'There     is a way'.title()'There     Is A Way'

如果句子以文章开头,则不希望标题的第一个单词小写。

请记住以下几点:

import re def title_except(s, exceptions):    word_list = re.split(' ', s)       # re.split behaves as expected    final = [word_list[0].capitalize()]    for word in word_list[1:]:        final.append(word if word in exceptions else word.capitalize())    return " ".join(final)articles = ['a', 'an', 'of', 'the', 'is']print title_except('there is a    way', articles)# There is a    Wayprint title_except('a whim   of an elephant', articles)# A Whim   of an Elephant


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号