字符串详解

没那么多废话,直接介绍字符串使用。继续往下看~~~

字符串定义:
*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) 查看字符串的用法

>>> 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中的字符串属于可迭代对象,通过一些方法可以遍历字符串中的每一个字符.而查找的方法主要有两个:find与index. 1.字符串的遍历 字 ...

  2. python基本数据类型之字符串(四)

    python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值 ...

  3. python基本数据类型之字符串(三)

    python基本数据类型之字符串(三) 转换和判断方法 在python中,有一些内置方法可以将字符串转化特定形式,而与之对应的一些方法可以判断字符串是否符合某些形式.因此,在这篇文章中,笔者把转换方法 ...

  4. python基本数据类型之字符串(二)

    python基本数据类型之字符串(二) 替换方法 python中字符串的替换方法主要有:center.rjust\ljust.expandtabs.format\format_map(格式化).str ...

  5. Python基础数据类型之字符串

    Python基础数据类型之字符串 一.Python如何创建字符串 在python中用引号将一些文本包起来就构成了字符串(引号可以是单引号.双引号.单三引号,双三引号,它们是完全相同的) >> ...

  6. 大爽Python入门教程 2-3 字符串,列表,字典

    大爽Python入门公开课教案 点击查看教程总目录 除了通用的序列方法, 列表和字符串还有些自己的专属方法. 后面介绍有些是英中文对照介绍(英文来自官方文档), 便于大家更深入的去理解其意思. 灵活的 ...

  7. python自学笔记(二)python基本数据类型之字符串处理

    一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...

  8. Python入门-数据类型

    一.变量 1)变量定义 name = 100(name是变量名 = 号是赋值号100是变量的值) 2)变量赋值 直接赋值 a=1 链式赋值  a=b=c=1 序列解包赋值  a,b,c = 1,2,3 ...

  9. Python基本数据类型之字符串、数字、布尔

     一.数据类型种类 Python中基本数据类型主要有以下几类: Number(数字) String(字符串) Bool (布尔) List(列表) Tuple(元组) Sets(集合) Diction ...

随机推荐

  1. SpringCloud高可用Eureka搭建

    网上很多博客写的都是在本地一台机器上面搭建的,我用两台机器来为大家搭建一个注册中心高可用集群 第一步:需要在每一台机器上面搭建一个注册中心. 第二步:编写第一台机器注册中心配置文件 第三步:编写第二台 ...

  2. poj 1035KINA Is Not Abbreviation

    题目链接:http://poj.org/problem?id=4054 本题的题意是在下面那部分待检验的单词中找到与之相对应的正确的代词,包含几种情况,一是全部字母相同,二是有一个字母不相同,三是多一 ...

  3. java 之 职责链模式(大话设计模式)

    目前很多OA办公自动化软件,加快了办公效率,简化流程.相信很多小伙伴都用过.笔者了解到的大多数办公软件底层实现流程大多数采用的都是Activity或者是JBPM框架. 今天笔者要说的也是类似于流程的一 ...

  4. POI tools 参数化生成excel表格

    package com.eccom.neteagle.server.confsave.service.impl; import java.io.File; import java.io.FileNot ...

  5. LeetCode :My solution N-Queens

    N-Queens Total Accepted: 15603 Total Submissions: 60198My Submissions The n-queens puzzle is the pro ...

  6. Testin云測试破7000万次:崩溃成90%手游应用质量难题

    Testin云測试破7000万次:崩溃成90%手游应用质量难题 2014/11/13 · Testin · 业界资讯 11月13日.全球最大的移动游戏.应用真机和用户云測试平台Testin云測宣布,已 ...

  7. or1200处理器的异常处理类指令介绍

    下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 我们在计算机体系结构的学习中知道:中断实质上包含由外部事件引起的硬中断(又称外中断)和由内部预先安排的特定指令或内部异常引起的软中断 ...

  8. redhat5安装Oracle11g

    redhat5安装Oracle11g 测试环境redhat5.5 oracle11g VMware 虚拟机 一.linux系统安装 二.下载oracle安装包 (我们需要把oracle安装包上传到li ...

  9. js中常见的一些兼容性问题

    1)滚动条: document.documentElement.scrollTop||document.body.scrollTop 2) 网页可视区域兼容 window.innerHeight || ...

  10. Mixed Reality-宁波市VR/AR技术应用高研班总结

    年,全球AR与VR市场规模将达到1500亿美元,而根据市场研究机构BI Intelligence的统计,2020年仅头戴式VR硬件市场规模将达到28亿美元,未来5年复合增长率超过100%.本次培训从V ...