Python随笔-字符串
- 函数title、lower、upper。
ct = "hello WORLD"
print(ct.title()) #title 以首字母大写的方式显示每个单词
print(ct.lower())
print(ct.upper())
# 结果:
# Hello World
# hello world
# HELLO WORLD
- python 用+拼接字符串。
ct = "hello WORLD"
s = ct + "!"
print(s)
# 结果:
# hello WORLD!
- 删除两侧空白strip。
s1 = ' s1 hello world '
s2 = "!!!!!!!!!s2 hello world!!!!!!!!!!!!"
#删除字符串右边的字符,默认为空白
print(s1.rstrip() + "|")
print(s2.rstrip("!") + "|")
#删除字符串左边的字符,默认为空白
print(s1.lstrip() + "|")
print(s2.lstrip("!") + "|")
#删除字符串两边的字符,默认为空白
print(s1.strip() + "|")
print(s2.strip("!") + "|")
# 结果:
# s1 hello world|
# !!!!!!!!!s2 hello world|
# s1 hello world |
# s2 hello world!!!!!!!!!!!!|
# s1 hello world|
# s2 hello world|
- 字符串运算符
| + | 字符串连接 |
>>>a+b 'HelloPython' |
| * | 重复输出字符串 |
>>>a*2 "HelloHello" |
| in | 成员运算符-如果字符串中包含给定的字符返回True |
>>>"H" in a True |
| r/R | 原始字符串,不进行转义 |
>>>print(r"\nhh") \nhh |
- python支持用三引号表达复杂的字符串
# python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。
ss = '''
line1
line2
line3
'''
print(ss)
Python随笔-字符串的更多相关文章
- [Python随笔]>>字符串大小写是如何转换的?
首先看下Python的源码 Emmmm,说明是底层的C实现的,所以只放了说明 再看看别人家孩子的博客:https://blog.csdn.net/world6/article/details/6994 ...
- Python格式化字符串~转
Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...
- python学习--字符串
python的字符串类型为str 定义字符串可以用 ‘abc' , "abc", '''abc''' 查看str的帮助 在python提示符里 help(str) python基于 ...
- Python格式化字符串和转义字符
地址:http://blog.chinaunix.net/uid-20794157-id-3038417.html Python格式化字符串的替代符以及含义 符 号 说 明 ...
- [转载] python 计算字符串长度
本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...
- Python基础-字符串格式化_百分号方式_format方式
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- python判断字符串
python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...
- Python格式化字符串
在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的pr ...
- python(七)字符串格式化、生成器与迭代器
字符串格式化 Python的字符串格式化有两种方式:百分号方式.format方式 1.百分号的方式 %[(name)][flags][width].[precision]typecode (name) ...
随机推荐
- 日期工具类 DateTools
为了跟其他日期工具类进行区分起名字DateTools public class DateTools { /** The DAT e_ forma t1. */ public static String ...
- Codeforces Round #240 (Div. 2) C Mashmokh and Numbers
, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge ...
- 基本的数据类型分析----java.lang.Number类及其子类分析
本文转自http://blog.csdn.net/springcsc1982/article/details/8788345 感谢作者 编写了一个测试程序,如下: int a = 1000, b= 1 ...
- sql 分组取每组的前n条或每组的n%(百分之n)的数据
sql 分组取每组的前n条或每组的n%(百分之n)的数据 sql keyword: SELECT * ,ROW_NUMBER() OVER(partition by b.UserID order by ...
- Unity3D开发——LeRunning的人物角色信息的显示
///////////////////////2015/08/22/////////////// //////////////////////by xbw/////////////////// ...
- HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求。
HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求. 2018-04-20 14:00 by 码农小周, 21 阅读, 2 评论, 收藏, 编辑 ...
- Wordpress的安装 for mac
1.服务器环境要求 PHP 5.2.4或更新版本 MySQL 5.0或更新版本 Apache mod_rewrite模块(可选,用于支持“固定链接”和“站点网络”功能) 选择安装:XAMPP 2.XA ...
- P4700 算
P4700 算 时间: 1000ms / 空间: 125829120KiB / Java类名: Main 背景 zhx和他的妹子出去玩. 描述
- pygame save that Stream as video output.
python - how to save pygame camera as video output - Stack Overflow https://stackoverflow.com/quest ...
- typescript 入门例子 Hello world——ts就是一个宿主机语言
安装 TypeScript TypeScript 的命令行工具安装方法如下: npm install -g typescript 安装完成之后,就有了 tsc 命令.编译一个 TypeScript 文 ...