Task 1 : 首字母大写

import re     #python 正则表达式包:re
s='hello world'
s=re.sub(r"\w+",lambda match:match.group(0).capitalize(),s)

赏析:

re.sub,实现正则的替换。

re.sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is, \n is converted to a single newline character, \r is converted to a carriage return, and so forth. Unknown escapes such as \j are left alone. Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern. For example: >>>
>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
... r'static PyObject*\npy_\1(void)\n{',
... 'def myfunc():')
'static PyObject*\npy_myfunc(void)\n{'
If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string. For example: >>>
>>> def dashrepl(matchobj):
... if matchobj.group(0) == '-': return ' '
... else: return '-'
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
'pro--gram files'
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
'Baked Beans & Spam'
The pattern may be a string or an RE object. The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous match, so sub('x*', '-', 'abc') returns '-a-b-c-'. In string-type repl arguments, in addition to the character escapes and backreferences described above, \g<name> will use the substring matched by the group named name, as defined by the (?P<name>...) syntax. \g<number> uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a replacement such as \g<2>0. \20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character ''. The backreference \g<0> substitutes in the entire substring matched by the RE. Changed in version 2.7: Added the optional flags argument.

Pattern  :  r"w+"表示匹配数字和字母下划线的多个字符。

repl  : lambda match:match.group(0).capitalize()表示首字母大写。

[Python]编程之美的更多相关文章

  1. Python编程之美:最佳实践指南PDF高清完整版免费下载|百度云盘|Python新手到进阶

    百度云盘:Python编程之美:最佳实践指南PDF高清完整版免费下载 提取码:1py6 内容简介 <Python编程之美:最佳实践指南>是Python用户的一本百科式学习指南,由Pytho ...

  2. 编程之美2014挑战赛 复赛 Codehunt平台试题答案

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  3. LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)

    题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  4. 编程之美2.5:寻找最大的K个数

    编程之美2.5:寻找最大的K个数 引申:寻找第k大的数: 方法一: // 选择第k大的数(通过改进快速排序来实现) public static void SelectShort(int[] array ...

  5. 24点C++程序实现 编程之美1.16

    解法1,对于任意输入的四个数字,给出一个24点的解法,若无解,则没有输出. 原理参照下图(编程之美原书) 代码如下,仅供参考 // 1.16.cpp : Defines the entry point ...

  6. 2017“编程之美”终章:AI之战勇者为王

    编者按:8月15日,第六届微软“编程之美”挑战赛在选手的火热比拼中圆满落下帷幕.“编程之美”挑战赛是由微软主办,面向高校学生开展的大型编程比赛.自2012年起,微软每年都在革新比赛命题.紧跟时代潮流, ...

  7. Python灰帽子:黑客与逆向工程师的Python编程之道PDF高清完整版免费下载|百度云盘

    百度云盘免费下载:Python灰帽子:黑客与逆向工程师的Python编程之道PDF高清完整版免费下载 提取码:8nki 目录  · · · · · · 第1章 搭建开发环境 11.1 操作系统要求 1 ...

  8. 3、Python编程之MySQLdb模块(0602)

    解释器环境与选项 python解释器启动 python [options] [ -c cmd | filename | - ] [ args ] python解释器环境变量 python代码的测试.调 ...

  9. python编程之禅

    在python界面输入 import this >>> import this The Zen of Python, by Tim Peters Beautiful is bette ...

随机推荐

  1. VC++ 使用attributes定义接口

      1.定义预处理命令_ATL_ATTRIBUTES 2.在一个全局的Cpp文件里面配置module的attribute [module(dll, uuid = "{3845951F-15B ...

  2. Log4j详细介绍(五)----输出地Appender

    Appender表示日志输出到什么地方,常用的输出地有控制台,文件,数据库,远程服务器等.Log4j中内置了常用的输出地,一般情况下配置一下即可使用.所有的Appender都实现自org.apache ...

  3. Java8 利用Lambda处理List集合循环给另外一个List赋值过滤处理

    1.利用stream().forEach()循环处理List; List<String> list = Lists.newArrayList();//新建一个List 用的google提供 ...

  4. HOW TO REPLACE ALL OCCURRENCES OF A CHARACTER IN A STD::STRING

    From: http://www.martinbroadhurst.com/replacing-all-occurrences-of-a-character-in-a-stdstring.html T ...

  5. chrome 浏览器的插件权限有多大?

    转自:https://segmentfault.com/q/1010000003777353 1)Chrome插件本身有机制控制,不会无限制的开放很多权限给你2)页面的DOM元素时可以操作的,Chro ...

  6. RPC简述

    在某种意义上,WebService.REST均是RPC的实现,那么RPC的发展过程如何呢?本文参考了wikipedia,对RPC做一下简要摘记. RPC(RemoteProcedureCall),是进 ...

  7. eclipse:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...

  8. SSL证书问题汇总

     //SLL协议分析 注意:对SSL协议的功能 https://www.myssl.cn/tools/check-server-cert.html //SSL证书格式转换 https://www.it ...

  9. Mac下用zsh

    最近好多实验要跑,有时候Finder切换来切换去,感觉还不如用terminal. Mac默认的shell是bash.所以说我其实今天才弄明白shell和terminal之间的关系.在人和计算机内核之间 ...

  10. Dockerfile 构建kibana 反向代理应用做用户认证访问

    FROM centos MAINTAINER z*****ch.cn RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime & ...