• 函数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随笔-字符串的更多相关文章

  1. [Python随笔]>>字符串大小写是如何转换的?

    首先看下Python的源码 Emmmm,说明是底层的C实现的,所以只放了说明 再看看别人家孩子的博客:https://blog.csdn.net/world6/article/details/6994 ...

  2. Python格式化字符串~转

    Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...

  3. python学习--字符串

    python的字符串类型为str 定义字符串可以用 ‘abc' , "abc", '''abc''' 查看str的帮助 在python提示符里 help(str) python基于 ...

  4. Python格式化字符串和转义字符

    地址:http://blog.chinaunix.net/uid-20794157-id-3038417.html Python格式化字符串的替代符以及含义     符   号     说     明 ...

  5. [转载] python 计算字符串长度

    本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...

  6. Python基础-字符串格式化_百分号方式_format方式

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  7. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

  8. Python格式化字符串

    在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的pr ...

  9. python(七)字符串格式化、生成器与迭代器

    字符串格式化 Python的字符串格式化有两种方式:百分号方式.format方式 1.百分号的方式 %[(name)][flags][width].[precision]typecode (name) ...

随机推荐

  1. maven profile多环境自动切换配置,配置分离,排除文件

    痛点: 在java开发的过程中,我们经常要面对各种各样的环境,比如开发环境,测试环境,正式环境,而这些环境对项目的需求也不相同. 在此之前,我们往往需要手动去修改相对应的配置文件然后打成war,才能部 ...

  2. HDU 4906 (dp胡乱搞)

    The Romantic Her Problem Description There is an old country and the king fell in love with a devil. ...

  3. 转载 - Vim 的 Python 编辑器详细配置过程 (Based on Ubuntu 12.04 LTS)

    出处:http://www.cnblogs.com/ifantastic/p/3185665.html Vim 的 Python 编辑器详细配置过程 (Based on Ubuntu 12.04 LT ...

  4. spring mvc参数校验

    一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...

  5. SfM环境的搭建windows8.1+vs2010

    SfM即Structure form Motion,这个算法的实现,作者Noah Snavely给出了一个具体的实现. 目前最新下载https://github.com/snavely/bundler ...

  6. windows bat命令 开启关闭Oracle服务

    0.吐槽 单位发的ThinkPad T61.太弱小了. 问题是我去百度下T61,发现它好贵好贵.真心无力吐槽.还不如给我发台外星人,廉价点的. . Oracle一开就内存就不够了.所以绝对不能让它开机 ...

  7. java中打印数组的5种方法

    Arrays.toString(arr) for(int n: arr) System.out.println(n+", "); for (int i = 0; i < ar ...

  8. UDEV SCSI Rules Configuration for ASM in Oracle Linux 5 and 6

    UDEV SCSI Rules Configuration for ASM in Oracle Linux 5 and 6 For Oracle Automatic Storage Manager ( ...

  9. 我所不知的 javascript Object 的一些现象

    1.我们都知道对象访问属性有两种方法,一种是使用 . 操作符,使用它添加的属性名必须满足标识符的命名规范:另一种是使用 [] 键访问的方法,可以接受任意UTF-8/Unicode字符串作为属性名,在E ...

  10. Can JavaScript connect with MySQL? 浏览器控制台的js采集数据结果持久化存储

    浏览器控制台的js采集数据结果持久化存储 how to open a file in javascript https://developer.mozilla.org/en-US/docs/Web/A ...