创建字符串

一对单引号或双引号

>>> '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. 创建Aurelia项目

    什么是Aurelia? Aurelia 是一个新的开源的,基于web标准的mvvm框架,是一个现代化的js模块的集合. Aurelia提供了丰富的plugin,例如国际化,验证,模态框,UI可视化等. ...

  2. 理解Promise的3种姿势

    译者按: 对于Promise,也许你会用了,却并不理解:也许你理解了,却只可意会不可言传.这篇博客将从3个简单的视角理解Promise,应该对你有所帮助. 原文: Three ways of unde ...

  3. spring boot之hello

    自己使用springboot也已经写过一段时间的代码,但是对springboot真正运行的流程还是有点模糊,今天写出自己对springboot的认识,如有不对,还请各位大佬不吝赐教,话不多说,直接上代 ...

  4. angularjs+webapi2 跨域Basic 认证授权(一)

    如今的app,利用各种前端框架结合html5的混合开发模式已然盛极一时.其中ionic+angularjs更是如日中天.这种模式利用angularjs $http 请求数据api 以达到前后端分离深得 ...

  5. 年会抽奖 抽奖系统 抽奖软件 C# Winform

    年会抽奖软件: Q.Q 358189777 C#.  数据库Access: 1.系统启动,自动全屏展示. 2.背景随心切换. 3.快捷键方便自如: F1:弹出设置界面 F2:查询人员名单.中奖名单 F ...

  6. angular 分页插件的使用

    html: <pagination total-items="totalItems" ng-model="currentPage" items-per-p ...

  7. [20181031]12c 在线移动数据文件.txt

    [20181031]12c 在线移动数据文件.txt --//12c以前,移动或者改名数据文件是一项比较麻烦的事情,至少要停一下业务.而12c支持在线移动或者改名数据文件,并且有点不可思议--//的是 ...

  8. SQL SERVER 查询表的各字段长度

    SELECT a.name,b.name,c.DATA_TYPE,b.max_length FROM sys.tables a join sys.columns b on b.object_id = ...

  9. c/c++ 有向无环图 directed acycline graph

    c/c++ 有向无环图 directed acycline graph 概念: 图中点与点之间的线是有方向的,图中不存在环.用邻接表的方式,实现的图. 名词: 顶点的入度:到这个顶点的线的数量. 顶点 ...

  10. AI学习---数据读取&神经网络

    AI学习---数据读取&神经网络 fa