Python学习笔记(3)-字符串

创建字符串
一对单引号或双引号
>>> '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)-字符串的更多相关文章
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
- Python学习笔记3—字符串
原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...
- 【Python学习笔记】字符串操作
字符串的表示 python中的字符串是一个常量,可以使用单引号'',双引号""或三引号""" """来创建一个字符串常量 ...
- Python学习笔记:字符串
字符串 字符串定义:字符串可以使用一对单引号.双引号或三引号来定义,即便是单个字符也会当做字符串来处理(Python中没有字符类型,单个字符也就是只有一个字符的字符串而已). 原始字符串:字符串中反斜 ...
- python学习笔记(一)---字符串与列表
字符串的一些处理 字符串的大小写 name="lonmar hb" print(name.upper())#全大写 print(name.lower())#全小写 print(na ...
- 【Python学习笔记】字符串拼接方法(5种)总结
字符串的 5 种拼接方法: “+”号 “,”号 直接连接 格式化 多行字符串拼接 第一种:“+”号 print("Hello"+"Python") 打印结果: ...
- python学习笔记(二)-字符串方法
python的字符串内建函数: #====================常用方法=============================name = 'besttest' new_name = n ...
- Python学习笔记----操作字符串
1.字符串相加.列表相加.列表和字符串不能混着使用 #序列相加 a="hello" b="python" c=a+b print("字符串相加的结果& ...
- Python学习笔记之字符串
一.字符串格式化 >>> format="Hello,%s. %s enough for ya?" >>> values=('World','H ...
随机推荐
- 初学HTML-1
HTML:Hyper Text Markup Language的缩写 超文本标记语言,用来描述文本的语义,这些文本———超文本,也叫标签. 基本格式: <html> <head ...
- Html中的img标签 加载失败
在Http请求时,有时会遇到img图片标签加载失败,不显示的情况: 解决方法,在重新给src属性赋值时,先将onerror事件清除掉,再赋值,这样就不会存在循环调用问题了,代码如下; <img ...
- CSS3动画属性:动画(animation)
一:动画(animation)的参数详解 由于上面用到了animation动画,这里详细介绍下这个animation的参数. 简介 CSS动画(Animations)简单说就是在一段固定的动画时间内暗 ...
- [IB]Integration Broker 是如何处理传入的请求(Part 2)
让我们讨论一下集成代理如何处理传入的请求.可以先阅读(理解PeopleSoft集成代理 Part1). 当PeopleSoft集成代理接收传入请求时,会发生一系列事件. 当侦听连接器(Listenin ...
- django rest framework 的xadmin 的坑
1.ImportError: No module named xadmin 方案: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath ...
- git 入门教程之 git 私服搭建教程
git 私服搭建教程 前几节我们的远程仓库使用的是 github 网站,托管项目大多是公开的,如果不想让任何人都能看到就需要收费,而且 github 网站毕竟在国外,访问速度太慢,基于上述两点原因,我 ...
- tkinter之grid布局管理器详解
在很久之前,我发过一篇<tkinter模块常用参数>,里面已经几乎涵盖了tkinter的大部分教程. 好吧,其实也就是上一篇而已啦. 所谓布局,就是指控制窗体容器中各个控件(组件)的位置关 ...
- Hive之FAILED: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient异常
一.场景 Hive启动不报错,当使用show functions;或create table...时报:FAILED: SemanticException org.apache.hadoop.hive ...
- java----OO的概念和设计原则(转)
一.OO(面向对象)的设计基础 面向对象(OO):就是基于对象概念,以对象为中心,以类和继承为构造机制,充分利用接口和多态提供灵活性, 来认识.理解.刻划客观世界和设计.构建相应的软件系统.面向对象的 ...
- Spring MVC 静态资源处理 (三)
完整的项目案例: springmvc.zip 目录 实例 项目结构: 一.配置web.xml <?xml version="1.0" encoding="UTF-8 ...