创建字符串

一对单引号或双引号

>>> 'hello world'

'hello world'

>>> "hello world"

'hello world'

可以字符串开始的引号之前加上r,忽略所有转义字符

三元引号,创建多行字符串,所有引号、制表符、换行都是字符串的一部分,可以作多行注释

>>> print('''

你好

    蔡威

     再见''')

 

你好

    蔡威

     再见

使用str()进行类型转换

可以将Python数据类型转换为字符串

拼接

>>> 'caiwei'+','+'hello'

'caiwei,hello'

复制

>>> '我爱你'*3

'我爱你我爱你我爱你'

提取字符,下标和切片

>>> a = 'helloworld'

>>> a[0]

'h'

>>> a[::2]

'hlool'

in 和 not in

>>> 'hello' in 'helloworld'

True

>>> 'caiwei' in 'helloworld'

False

字符串方法

长度len()

>>> len('hello')

大小写lower() upper()

所有字符都变成大写或小写

>>> 'HEllo'.lower()

'hello'

>>> 'HEllo'.upper()

'HELLO'

isX方法

islower() isupper()大小写

isalpha()字母

isalnum()数字和字母

isdecimal()数字

isspace()转义字符

判断开始或结束部分是否等于另一个字符串startswith() endswith()

>>> 'helloworld'.startswith('he')

True

>>> 'helloworld'.startswith('ll')

False

>>> 'helloworld'.endswith('world')

True

>>> 'helloworld'.endswith('he')

False

字符串和列表 join()和split()

>>> 'hello,world'.split(',')

['hello', 'world']

>>> ','.join(['hello', 'world'])

'hello,world'

对齐文本rjust(),ljust()和center()

第一个参数在这个字符串个数

第二个参数是指填充字符

>>> a.rjust(20,'*')

'**********helloworld'

>>> a.ljust(20,'*')

'helloworld**********'

>>> a.center(20,'*')

'*****helloworld*****'

删除空白字符lstrip() rstrip() strip()

可以设置参数,默认空格

>>> a = ' hello world '

>>> a.lstrip()

'hello world '

>>> a

' hello world '

>>> a.rstrip()

' hello world'

>>> a

' hello world '

>>> a.strip()

'hello world'

>>> a

' hello world '

替换 replace()

第一个参数是要修改字符串,

第二个参数是传入字符串

>>> a.replace('world','caiwei')

' hello caiwei '

复制粘贴字符串pyperclip模块

>>> import pyperclip as py

>>> py.copy('hello world')

>>> py.paste()

'hello world'

Python学习笔记(3)-字符串的更多相关文章

  1. python学习笔记(字符串操作、字典操作、三级菜单实例)

    字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...

  2. Python学习笔记3—字符串

    原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...

  3. 【Python学习笔记】字符串操作

    字符串的表示 python中的字符串是一个常量,可以使用单引号'',双引号""或三引号""" """来创建一个字符串常量 ...

  4. Python学习笔记:字符串

    字符串 字符串定义:字符串可以使用一对单引号.双引号或三引号来定义,即便是单个字符也会当做字符串来处理(Python中没有字符类型,单个字符也就是只有一个字符的字符串而已). 原始字符串:字符串中反斜 ...

  5. python学习笔记(一)---字符串与列表

    字符串的一些处理 字符串的大小写 name="lonmar hb" print(name.upper())#全大写 print(name.lower())#全小写 print(na ...

  6. 【Python学习笔记】字符串拼接方法(5种)总结

    字符串的 5 种拼接方法: “+”号 “,”号 直接连接 格式化 多行字符串拼接 第一种:“+”号 print("Hello"+"Python") 打印结果: ...

  7. python学习笔记(二)-字符串方法

    python的字符串内建函数: #====================常用方法=============================name = 'besttest' new_name = n ...

  8. Python学习笔记----操作字符串

    1.字符串相加.列表相加.列表和字符串不能混着使用 #序列相加 a="hello" b="python" c=a+b print("字符串相加的结果& ...

  9. Python学习笔记之字符串

    一.字符串格式化 >>> format="Hello,%s. %s enough for ya?" >>> values=('World','H ...

随机推荐

  1. git参考, 小结

    git官网: https://git-scm.com 菜鸟教程: http://www.runoob.com/git/git-tutorial.html 廖雪峰: https://www.liaoxu ...

  2. K8S dashboard

    kubernetes-dashboard有两种认证方式,一个token认证,另一个是Kubeconfig文件的认证.这个时候的认证不是UserAccount而是获取kubernetes集群资源信息的s ...

  3. JavaScript Array常用属性和方法

    Array的length属性可以通过赋值改变,但这样会导致Array原有的大小发生改变. var a = ["I", "Love", "You&quo ...

  4. POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 13954   Accepted: 4673 Description Bein ...

  5. Java NIO 学习

    Java NIO提供了与标准IO不同的IO工作方式: Channels and Buffers(通道和缓冲区):标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(B ...

  6. ReactNative调研结果

    React Native相关调研总结 一.概要 React Native - 使用React开发世界一流的原生应用: 使用JavaScript和React(对JS有一定扩展)作为开发语言: React ...

  7. ionic开发中,输入法键盘弹出遮挡住div元素

    采用ionic 开发中,遇到键盘弹出遮挡元素的问题. 以登陆页面为例,输入用户名和密码时,键盘遮挡了登陆按钮. 最终采用自定义指令解决了问题: .directive('popupKeyBoardSho ...

  8. Android 进度条按钮实现(ProgressButton)

    有些App在点击下载按钮的时候,可以在按钮上显示进度,我们可以通过继承原生Button,重写onDraw来实现带进度条的按钮. Github:https://github.com/imcloudflo ...

  9. webpack项目轻松混用css module

    前言 本文讲述css-loader开启css模块功能之后,如何与引用的npm包中样式文件不产生冲突. 比如antd-mobilenpm包的引入.在不做特殊处理的前提下,样式文件将会被转译成css mo ...

  10. 转:RowVersion 用法

    在数据表更新时,如何表征每个数据行更新时间的先后顺序?最简单的做法是使用RowVersion(行版本)字段,它和时间戳(TimeStamp)类型的功能相似,只不过TimeStamp 已过时,应避免用于 ...