Python第一天——入门Python(2)字符串的简单操作
数据的操作
字符串的一些常用操作:
1
1 #!/usr/bin/env python
2 # #coding=utf-8
3 #
4 # test='hello world'
5 # print(test.capitalize()) #首字母大写
6 # print (test)
7 #
8 # ###test.capitalize这个参数的运行结果并不会影响test的值。所以下面的print(test)的输出结果还是helloword
2
# test='hello world' # print(test.center(50)) #居中 # print(test.center(50,'*')) #居中加填充
3
# test='hello world'
# print(test.count('l')) #查找字符串中出现的字符次数,有则输出。没有则输出0
# print (test.count('l',0,2)) #查找字符串中出现的字符次数,并设置启始位置与结束位置
4
# test='hello world'
# print(test.endswith('l')) #判断字符串是以什么字符结尾
# print(test.endswith('d'))
#
# print (test.startswith('d')) #判断字符串是以什么字符开头
# print(test.startswith('h'))
5
# test='hello world'
# print(test.find('z'))
# print(test.find('o')) #查找字符串中的子字符是否存在,存在则输出字符串序列位置。没有则返回-1
# print(test.find('l',0,7)) #设置起始位置与结束位置,并查找字符
6
# test='hello world'
# print(test.index('l')) #index与find区别:find在查找,找不到则返回-1而index是在已知的情况在进行查找,找不到则报错
7
# test='hello world'
# print(test.isdigit())
# test1='12345687890'
# test1='aaa1234'
# print (test1.isdigit()) #查看字符串中是否由纯数字组成
8
# test='hello world' # test_new='*'.join(test) # print (test_new) #在两个字符之间加入其它字符
9
# test='root:x:0:0:root:/root:/bin/bash'
# print(test.split(':'))
# print (test.split(':',maxsplit=1)) #按冒号切割字符。maxsplit=? '?'表示最大分隔几次
#
# test_list=test.split(':')
# print (':'.join(test_list)) #与join相交互调用
10
# test='HELLO WORLD' # print (test.upper()) #小写转换成大写 # print(test.isupper()) #判断字符串是否为大写
11
# test=' hellO world x'
# print(test.strip('')) #strip去掉开头与结尾字符。冒号里为空则删除空格
# print(test.strip('x')) #x并不是在开头或者结尾位置,则不删除任何字符 # print(test.rstrip('x')) #去掉右边不去掉左边
# print(test.lstrip(' ')) #去掉左边不去掉右边
12
# test='hello world'
# print(test.replace(' ','',1)) #replace去掉中间的字符,例如两个空格若要后面不加参数则统统替换
字符串其它不常用的方法
13
# test='hello world'
# test1='hello123world'
# test2='helloworld'
# print(test.isalpha())
# print (test1.isalpha())
# print (test2.isalpha()) #判断字符串是否是纯字母结构,如果是则返回True 否则返回False
14
# test='hello world'
# test1='type'
# print(test.isidentifier())
# print(test1.isidentifier()) #判断字符串是否为内置标识符
15
# test='hello world'
# test1="HELLO WORLD"
# print(test.isupper())
# print(test1.isupper()) #判断字符串是否为大写
16
# test='hello world'
# print(test.isprintable()) #判断是否可以被打印 没什么卵用
17
# test='hello world'
# test1=' '
# print(test.isspace())
# print(test1.isspace()) #判断字符串是否由空格组成
18
# test='hello world'
# test1=' '
# print(test.islower())
# print(test1.islower()) #判断字符串中是否有字母,有则返回True 没有则返回False
19
# test='Hello world' # print(test.istitle()) #判断字符串是否已大写字母开头,是则返回True 没有则返回False
20
# test='hello world'
# print(test.ljust(20,'*'))
# print (test.rjust(20,'*')) #左对齐填充字符 与右对齐填充字符
21
# test='''This is first line.
# This is two line.
# This is three line.
# '''
# print (test.splitlines()) #将整段字符串按换行切
22
test='hello world' print (test.zfill(20)) #设置字符串宽度,不足在左侧填零
字符串属于序列类型 。序列又称为有序排列的意思 所以字符也是有序排列的,那么怎么操作呢!各位往下看
切记python计数是从零开始,所以第一个字符为0,第二个字符为1,第三个字符为2,..... 等等。
字符串的索引操作
1
1 # test='hello'
2 # print (test[0]) #切出第一个字符
3 # print(test[4]) #切出第四个字符
4 # print(test[-1]) #负数是从右往左数,-1就是倒数第一个,-2就是倒数第二个等等
5
6 # test='hello'
7 # print(test[1000]) #如果索引超出序列 则报错
字符串的切分操作
2
1 # test='hello world'
2 # print(test[0:3]) #切片切出从0开始到第三个字符 但是在python 顾头不顾尾,即只切到序列2 切记切记!!!
3 # print(test) #对于字符串的任何操作 并不会改变原来的字符串,除非test=test(test[0:3])
4 # print(test[0:]) #从头切到尾,冒号后面不指定序列 将会切到最后
5 # print (test[:3]) #冒号前面如果不指定索引 则从0开始切
6 # print(test[0:5:2]) #从0开始切到5 步长为2,意思是隔一个字符切一个字符
7 # print(test[::-1]) #从后往前切,整个颠倒
字符串的遍历
3
test='hello world'
for i in test:7
print(i) #将字符串循环遍历出来
变量的解压
4
1 # test='hello'
2 # x,y,z='abc'
3 # print (x)
4 # print (y)
5 # print (z) #变量的解压
6 # x,y,z='abc','defss','xxx'
7 # print (x)
8 # print (y)
9 # print (z)
字符串的一些数学运算
不常用
# meg=''
# print (meg+'hello') #字符串的相加
# meg1='hello'
# print (meg1*10) #字符串相乘(字符串不可以进行减法与除法)
Python第一天——入门Python(2)字符串的简单操作的更多相关文章
- Python第一天——入门Python(1)数据定义
数据类型: 什么是数据? 在计算机科学中,数据是指所有能输入到计算机并被计算机程序处理的符号的介质的总称,是用于输入电子计算机进行处理,具有一定意义的数字字母.符号和模拟量等的统称.现在计算机存储和处 ...
- Python第一天——入门Python(3)列表
列表,也是一种序列类型. 如何定义列表? 用" [ ] "(中括号进行定义) 列表的索引操作 例如 # hobby_list=['basketball','football','p ...
- Python第一天——入门Python(4)字典的常用操作
# dic={[1,2,3]:'123'} #可变类型不能当做字典的key,value可以使用任意类型 # dic={(2,3,4):'123'} # print (dic[(2,3,4)]) #元组 ...
- python爬虫-基础入门-python爬虫突破封锁
python爬虫-基础入门-python爬虫突破封锁 >> 相关概念 >> request概念:是从客户端向服务器发出请求,包括用户提交的信息及客户端的一些信息.客户端可通过H ...
- Python 第一篇:python简介和入门
一.python简介 1.python下载地址:https://www.python.org/downloads/ Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆 ...
- python第一课——关于python的一些概念
day01(上午): 1.学习方法(建议): 1).不要依赖于我的视频,绝对不要晚上将视频全部在过一遍 2).上课不要记笔记,而且不要用纸质的笔记本去整理笔记 3).不要只看不敲,代码方面我们需要做到 ...
- Python字符串的简单操作
数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...
- python第一周:python初识、流程控制
编译性语言:在将源代码编译完毕生成一个可执行文件后才能运行 解释性语言:在代码的运行期间进行编译 动态类型语言:在运行期间才去做数据检查的语言,也就是说在使用动态类型语言时不用指定数据类型 静态类型语 ...
- Python第一天——初识Python
python是由荷兰人Guido van Rossum 于1989年发明的一种面向对象的的解释型计算机程序设语言,也可以称之为编程语言.例如java.php.c语言等都是编程语言. 那么为什么会有编程 ...
随机推荐
- C语言身份证信息查询系统(修改版)
很久以前写了一个<C语言身份证信息查询系统>,如果你点击链接进去看了. 估计也会被我那磅礴大气的代码震惊到的,最近复习/学习文件操作,把代码改了改,算是对以前还不会文件操作的时候的愿望,哈 ...
- C语言缓冲区清空
C语言中有几个基本输入函数: //获取字符系列 int fgetc(FILE *stream); int getc(FILE *stream); int getchar(void); //获取行系列 ...
- js实现tooltip动态提示效果(文字版)
页面中经常用到鼠标移动到一个元素上面显示提示的功能,最开始的做法是在下面创建一个div然后动态显示这个div,但是这样需要加很多div,比较麻烦. 针对上面个的需求,这边写了一个tooltip动态提示 ...
- iOS后向兼容:如何发现过期接口
以4.3以下兼容性为例,在项目预编译头文件(xx.pch)中加入如下代码: #import <Availability.h> #define __AVAILABILITY_INTERNAL ...
- 根据Eclipse SVN changelog使用ANT自动打增量包
1.获取changeLog 用eclipseSVN的插件功能查看history. 将日志文件导出到本地文件svn_change.log,格式如下 r63 | xiaodaoshi | 2014-08- ...
- POI操作EXCEL之导出Excel(设置有效性,下拉列表引用)
本人使用的是poi-bin-3.10-FINAL-20140208.zip 版本的poi以下是程序关键代码: //需要引用的类 import java.io.File; import java.io. ...
- SQLSERVER数据库学习总结七(视图,索引)
--视图的主意点:不能和表的名称相同,如果某一列为函数,表达式,常量或者与来自多张表的列名相同,必须为列定义名称,不能在试图上创建索引 if exists(select 1 from sys.syso ...
- 传说中的华为Python笔试题——两等长整数序列互换元素,序列和的差值最小(修正)
有两个序列a,b,大小都为n,序列元素的值任意整形数,无序:要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小. 1. 将两序列合并为一个序列,并排序,得到source ...
- 怎么让猫吃辣椒 转载自 xiaotie
典故: 某日,毛.周.刘三人聊天. 毛:怎么能让猫自愿吃辣椒? 刘:掐着脖子灌. 毛:强迫不是自愿. 周: 先饿几天,再混到猫爱吃的东西里. 毛:欺骗不是自愿.把辣椒涂到猫肛门上,它就会自己去舔了. ...
- SQL 语句中的union操作符
前端时间,用到了union操作符,周末有时间总结下,w3c手册内容如下: SQL UNION操作符 UNION操作符用于合并两个或多个select语句的结果集. 注意:UNION内部select语句必 ...