字符串详解

字符串的用法是最多的,很多功能的实现都离不开字符串,而且字符串的使用方法也很多,这里面不能说全部给大家一一介绍,只能说把一些常用的列举出来,方便回忆或者说供大家参考,谢谢!请继续往下看~~~

先看下字符串的内置方法有哪些?

>>> 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基础入门-字符串的更多相关文章

  1. Python基础入门教程

    Python基础入门教程 Python基础教程 Python 简介 Python环境搭建 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循 ...

  2. Python基础入门总结

    Python基础入门教学 基础中的基础 列表.元组(tuple).字典.字符串 变量和引用 函数 python视频教程下载 基础中的基础 解释型语言和编译型语言差距: Python概述 解释器执行原理 ...

  3. [新手必备]Python 基础入门必学知识点笔记

    Python 作为近几年越来越流行的语言,吸引了大量的学员开始学习,为了方便新手小白在学习过程中,更加快捷方便的查漏补缺.根据网上各种乱七八糟的资料以及实验楼的 Python 基础内容整理了一份极度适 ...

  4. Python基础数据类型-字符串(string)

    Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...

  5. python 字符串 - python基础入门(12)

    在 python变量 文章中我们对python变量做了一个简单的了解,整数/浮点数/bool值相对来讲都比较简单,今天详细在讲解一下关于字符串的内容,字符串俗称:str. 在本文会大量的使用print ...

  6. python基础入门之三 —— 字符串

    1.格式 一对引号和三对引号可以表示字符串 (三引号保留换行) 2.下标 从0开始循序向下分配 str1='abcdefg' print(str1) print(str1[0]) print(str1 ...

  7. Python基础入门教程,Python学习路线图

    给大家整理的这套python学习路线图,按照此教程一步步的学习来,肯定会对python有更深刻的认识.或许可以喜欢上python这个易学,精简,开源的语言.此套教程,不但有视频教程,还有源码分享,让大 ...

  8. Python怎么样入门?Python基础入门教程

    给大家整理的这套python学习路线图,按照此教程一步步的学习来,肯定会对python有更深刻的认识.或许可以喜欢上python这个易学,精简,开源的语言.此套教程,不但有视频教程,还有源码分享,让大 ...

  9. Python基础入门知识

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

随机推荐

  1. php MySQL使用rand函数随机取记录(转)

    php MySQL使用rand函数随机取记录 如何在mysql中使用随机数, 如何写一个语句能一下更新几百条MYSQL数据! 需要测试MYSQL数据库,里面有一个上万条数据的数据库,如何写一个PHP文 ...

  2. 插耳机对orientation sensor的影响

    偶然发现,耳机孔插上耳机对手机的磁感应器有非常大的影响. 具体影响的方式和解决的方法,慢慢研究下

  3. Socket代码

    服务器端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  4. git问题记录

    1.从远程仓库拉取A文件,在本地删掉了这个A文件,然后再去拉取远程仓库,是拉不下来的,因为本地版本库比远程库的高 我觉得是第一次远程的版本已经拉取了,本地已记录,远程程没有发生变化,在拉取和先前的一致 ...

  5. selenium中Xpath轴定位方法

    1.Xpath轴:轴可定义相对于当前节点的节点集. 使用语法:轴名称::节点名称 例://input[@data-value="SXRYNAME"]/parent::td/foll ...

  6. MFC调试的几个技巧

    TCHAR pStr[] = _T("this is a test!"); void* p = (void*)pStr; TRACE(_T("pStr is %s\n&q ...

  7. java成神之——集合框架之ArrayList,Lists,Sets

    集合 集合种类 ArrayList 声明 增删改查元素 遍历几种方式 空集合 子集合 不可变集合 LinkedList Lists 排序 类型转换 取交集 移动元素 删除交集元素 Sets 集合特点 ...

  8. springboot成神之——log4j2的使用

    本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...

  9. babel-preset-latest使用介绍

    本文介绍babel中的babel-preset-latest插件 简介 该插件的作用是包含了每年的js代码转换规则 默认包含es2015,es2016,es2017,默认对这些代码都会进行转码,从而方 ...

  10. mysql之数据类型以及操作数据表

    数据类型: 数据类型是指列.存储过程的参数.表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型. ———————————————————————————————————————— ...