Python基础入门-字符串
字符串详解
字符串的用法是最多的,很多功能的实现都离不开字符串,而且字符串的使用方法也很多,这里面不能说全部给大家一一介绍,只能说把一些常用的列举出来,方便回忆或者说供大家参考,谢谢!请继续往下看~~~
先看下字符串的内置方法有哪些?
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>
看完上面,字符串的内置方法还是蛮多的。那么我们看看他们是如何使用的。
字符串定义:
*1.引号包围,不可变(指的是不可以对字符串进行修改)得序列(凡是能够通过索引取值的都是序列)。
*2.不可变对象(字符串)在调用自身方法时,不会改变原有内容。
字符串创建:
' '
" "
""" 或者 '''
1.单引号和双引号,字符串可以嵌套使用,不可以交叉使用。
2.三引号经常用于多行注释
>>> print '''hello,jim my name is bob!
... how are you?
... welcome to china!
... byebye
... '''
输出结果:
hello,jim my name is bob!
how are you?
welcome to china!
byebye
3.字符串中的\转义字符
>>> 'let\'s go jim we are go to school!'
"let's go jim we are go to school!"
字符串中的\n
>>> print '''hello,zhaoming\nhow are you?'''
hello,zhaoming
how are you?
4.字符串之 原始字符串 r
>>> print r'C:\now\abc\bcd\efg\aaa\nnn\bmb'
C:\now\abc\bcd\efg\aaa\nnn\bmb
>>> 'C:\now'
'C:\now'
>>> print 'C:\now'
C:
ow
>>> print 'C:\\now'
C:\now
5.字符串拼接 (通过加号+拼接)
>>> name = "zhangshan"
>>> age = '25'
>>> name + age
'zhangshan25'
>>> name = "zhangshan"
>>> age = 25
>>> name + str(age)
'zhangshan25'
>>> name + str(age) + 'how are you?'
'zhangshan25how are you?
6.字符串格式化输出
1.通过% 格式化操作 %s替换所有类型 %d替换整型 %f替换浮点型
>>> name = 'lishi'
>>> age = 155
>>> 'hello,%s my age is %s'%(name,age)
'hello,lishi my age is 155'
>>> 'hello,%s my age is %s'%(age,name)
'hello,155 my age is lishi'
>>> 'hello,%d my age is %s'%(name,age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> 'hello,%s my age is %d'%(name,age)
'hello,lishi my age is 155'
7.判断变量类型内置函数
type(a)
isinstance(a,b) a为变量,b为类型 检查是否匹配 匹配为真 不匹配为假
>>> isinstance(111,int)
True
>>> isinstance('',int)
False
>>> isinstance('',str)
True
>>> isinstance(11.00,float)
True
8.字符串复制 *
>>> 'hello' + 'zhanshan' + 'nihao---->'*3
'hellozhanshannihao---->nihao---->nihao---->'
9.字符串判断
>>> 'zhang' in 'zhangshan'
True
>>> '12' in 'zhangshan'
False
>>> 'zhang' not in 'zhangshan'
False
10.字符串索引
索引从0开始,默认取值方向从左到右。单个取值
>>> 'hello_world'
'hello_world'
>>> 'hello_world'[0]
'h'
>>> 'hello_world'[1]
'e'
>>> 'hello_world'[2]
'l'
普通截取 [开始端:结束端] 包含开始端 不包含结尾端
>>> 'hello_world'
>>> demo[0:11]
'hello_world'
>>> demo[:]
'hello_world'
>>> demo[0:4]
'hell'
>>> demo[0:3]
'hel'
步长截取 [开始端:结束端:步长值] 包含开始端 不包含结尾端 步长按照步长值减1隔取
>>> demo = 'hello_world'
>>> demo[::2]
'hlowrd'
>>> demo[::]
'hello_world'
>>> demo[::2]
'hlowrd'
>>> demo[:5:2]
'hlo'
>>> demo[:5:2]
>>> demo[::3]
'hlwl'
反向截取 [开始端:结束端] 包含开始端 不包含结尾端
>>> demo = 'hello_world'
>>> demo[-1]
'd'
>>> demo[-2]
'l'
>>> demo[-3]
'r'
>>> demo[-5:-1]
'worl'
>>> demo[-5:0]
''
>>> demo[-5:]
'world'
特殊情况
>>> demo = 'hello_world'
>>> demo[5:0]
''
>>> demo[5:0]
''
>>> demo[0:12]
'hello_world'
>>> demo[0:13]
'hello_world'
>>> demo = 'hello_world'
>>> demo[-1:-5]
''
>>> demo[-3:-5]
''
>>> demo[-20:]
'hello_world'
11.字符串常用操作函数
dir(str) 查看字符串的用法
1.字符串-find()函数
可以查找字符串中的元素的索引位,如果查询一个不存在的元素返回值为-1,如果能查到指定元素
那么就会返回该元素对应的索引位。
>>> demo = 'hello_world'
>>> demo.find('e')
1
>>> demo.find('m')
-1
>>> demo = 'hello_werld'
>>> demo.find('e')
1
>>> demo.find('w')
6
2.字符串-split()函数(分隔函数)
>>> demo = 'hello_werld'
>>> demo.split('e')
['h', 'llo_w', 'rld']
>>> demo = 'hello_world'
>>> demo.split('e')
['h', 'llo_world']
>>> demo.split('_')
['hello', 'world']
>>> demo.split('')
3.字符串-upper()函数 将字符串中所有的字符变成大写
(不可变对象调用自身方法不会改变自身的原有内容)
>>> demo = 'hello_world'
>>> demo.upper()
'HELLO_WORLD'
>>> demo
'hello_world'
>>> abc=demo.upper()
>>> abc
'HELLO_WORLD'
>>> demo
'hello_world'
>>>
4.字符串-lower()函数 将字符串中所有的字符变成大写
>>> abc='HELLO_WORLD'
>>> abc
'HELLO_WORLD'
>>> abc.lower()
'hello_world'
>>>
5.字符串的替换-replace(a,b) a是要替换的原内容 b是替换的新内容
>>> url='www.baidu.com'
>>> url
'www.baidu.com'
>>> url.replace('baidu','sina')
'www.sina.com'
>>> url
'www.baidu.com'
>>> demo=url.replace('baidu','sina')
>>> demo
'www.sina.com'
6.字符串的- join函数(分隔)
>>> url='www.baidu.com'
>>> ':'.join(url)
'w:w:w:.:b:a:i:d:u:.:c:o:m'
>>> username='zhangshan,lishi,wangwu'
>>> ':'.join(username)
'z:h:a:n:g:s:h:a:n:,:l:i:s:h:i:,:w:a:n:g:w:u'
>>> username='zhangshan,lishi,wangwu'
>>> username.split(',')[0]
'zhangshan'
>>> username.split(',')[1]
'lishi'
>>> username.split(',')[2]
'wangwu'
7.字符串的strip 忽略字符串前后空格
>>> username=' zhangshan,lishi,wangwu '
>>> username
' zhangshan,lishi,wangwu '
>>> username.strip()
'zhangshan,lishi,wangwu'
(转载请标明出处,谢谢!)
Python基础入门-字符串的更多相关文章
- Python基础入门教程
Python基础入门教程 Python基础教程 Python 简介 Python环境搭建 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循 ...
- Python基础入门总结
Python基础入门教学 基础中的基础 列表.元组(tuple).字典.字符串 变量和引用 函数 python视频教程下载 基础中的基础 解释型语言和编译型语言差距: Python概述 解释器执行原理 ...
- [新手必备]Python 基础入门必学知识点笔记
Python 作为近几年越来越流行的语言,吸引了大量的学员开始学习,为了方便新手小白在学习过程中,更加快捷方便的查漏补缺.根据网上各种乱七八糟的资料以及实验楼的 Python 基础内容整理了一份极度适 ...
- Python基础数据类型-字符串(string)
Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...
- python 字符串 - python基础入门(12)
在 python变量 文章中我们对python变量做了一个简单的了解,整数/浮点数/bool值相对来讲都比较简单,今天详细在讲解一下关于字符串的内容,字符串俗称:str. 在本文会大量的使用print ...
- python基础入门之三 —— 字符串
1.格式 一对引号和三对引号可以表示字符串 (三引号保留换行) 2.下标 从0开始循序向下分配 str1='abcdefg' print(str1) print(str1[0]) print(str1 ...
- Python基础入门教程,Python学习路线图
给大家整理的这套python学习路线图,按照此教程一步步的学习来,肯定会对python有更深刻的认识.或许可以喜欢上python这个易学,精简,开源的语言.此套教程,不但有视频教程,还有源码分享,让大 ...
- Python怎么样入门?Python基础入门教程
给大家整理的这套python学习路线图,按照此教程一步步的学习来,肯定会对python有更深刻的认识.或许可以喜欢上python这个易学,精简,开源的语言.此套教程,不但有视频教程,还有源码分享,让大 ...
- Python基础入门知识
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
随机推荐
- 转 OpenFaaS 介绍
来源: https://thenewstack.io/openfaas-put-serverless-function-container/?utm_source=tuicool&utm_me ...
- bzoj 2119 股市的预测 —— 枚举关键点+后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2119 思路就是对于这个形如 ABA 的串,枚举 A 的长度,并按照长度分出几块,找到一些关键 ...
- 日志jar包冲突报错:Class path contains multiple SLF4J bindings
问题现象:tomcat启动卡死,报错日志如下: 十一月 07, 2017 8:35:45 下午 org.apache.catalina.core.ApplicationContext log 信息: ...
- bootstrap 设置表格固定宽度 内容换行
在项目中开发的时候用的bootstrap,但是有些表格的内容 会显示的很长 那么我第一时间想到的就是 修改td或者th的width,但是我设置了 之后不起作用 于是百度找到了解决方法: 学习源头: h ...
- 学习FPGA有必要写SDRAM控制器吗?
在学习FPGA的过程中,注意是在学习过程中,联系FPGA的使用技巧,强烈建议尝试设计一个SDRAM控制器,不要使用IP核. 学习SDRAM控制器设计,能让你掌握很多知识. 更好的使用状态机去精准控制时 ...
- 利用全局变量$_SESSION和register_shutdown_function自定义会话处理
register_shutdown_function 可以注册一个自定义的函数,在程序运行结束之前 执行. 在做ecshop的二次开发过程中,虽然代码 太老太乱太冗余,但ec的会话处理的设计感觉还是不 ...
- linux文件系统命令和分区 挂载
文件系统命令df [选项][挂载点]选项:-a 显示所有的文件系统信息,包括特殊文件,如/proc,/sysfs-h 使用习惯单位显示容量,如KB,MB或GB等-T 显示文件系统类型-m 以MB为单位 ...
- php小算法总结一(数组重排,进制转换,二分查找)
1.两个有序数组组合成一个新的有序数组 <?php $arr1=array(2,5,7,9,12); $arr2=array(3,4,6,8,10,11); function merge_sor ...
- python开发mysql:mysql数据类型&约束条件
一 整形 只有Int类型跟存储没有关系,显示的是宽度,其他类型都是限制 整形类型:[(m)][unsigned][zerofill] 作用:存储年龄,等级,id,各种号码 m,代表显示宽度 默认11 ...
- sql中left join on where区别剖析
select from tb1 left join tb2 on tb1.xx=tb2.xx and tb2.xxxx=5 先筛选tb2.xxxx=5 再把tb1与筛选后的临时表进行左连接. sele ...