19、python基础学习-字符串及操作

 1 #!/usr/bin/env python
 2 #__author: hlc
 3 #date: 2019/5/26
 4 # 字符串是以单引号‘或者双引号"括起来的任意文本,例如:‘asd‘,"123"
 5 # ‘"不是字符串的一部分,如果需要作为字符串的一部分,需要在单引号外面加上双引号,如:"it‘s a Good !"
 6
 7 #创建字符串
 8 # var1 = "Hello word"
 9 # var2 = ‘python book‘
10
11 #字符串操作
12 # 重复输出字符串
13 # print("hello"*2) #hellohello
14 #通过索引获取字符串中的字符
15 # print("hello"[2:]) # llo
16 #判断成员
17 # print("he" in "hello") # True
18 # print("he1" in "hello") # False
19 #格式化字符串
20 # name = "asd"
21 # print("your name is %s !"% name) #your name is asd !
22 #字符串拼接
23 # a = "qwe"
24 # b = "456"
25 # c = "uio"
26 # print(a+b) # qwe456,使用"+"进行拼接,效率低
27 #
28 # print(‘‘.join((a,b,c))) # qwe456uio ,效率高
29 # print(‘------‘.join((a,b,c))) # qwe------456------uio
30
31 # 字符串的内置方法
32 # st = "hello kitty"
33 # print(st.count("l")) # 2 ,统计元素个数
34 # print(st.capitalize()) # Hello kitty , 首字母大写
35 # print(st.center(20,"#")) # 将内容居中,其他用指定字符填充
36 # print(st.endswith("tty")) # True ,判断以某个内容结尾
37 # print(st.startswith("hell")) # True ,判断以某个内容开头
38 # st = "he\tllo kitty"
39 # print(st.expandtabs(20)) # he                  llo kitty ,将\t替换为指定的空格
40 # print(st.find("t")) # 8 ,查找到第一个元素,并将索引值返回,没有返回 -1
41 # st = "hello kitty {num} {sam}"
42 # print(st.format(sam = 100,num = 200)) # hello kitty 100 200 ,格式化输出
43 # print(st.format_map({"num":100,"sam":200})) # hello kitty 100 200 ,格式化输出
44 # print(st.index("t")) # 8,查找到第一个元素,并将索引值返回,和find相比,没有会报错
45 # print(st.isalnum()) # False 判断是否为字符串,数字,汉字
46 # print(st.isalnum()) # False 判断是否为字符串,数字,汉字
47 # print(st.isdecimal()) # False 判断是否为十进制
48 # print("1234".isdigit()) # True 判断是否为整数
49 # print("123.4".isdigit()) # False 判断是否为整字
50 # print("123.4".isnumeric()) # False 判断是否为整字,和isdigit功能一样
51 # print("Asd".islower()) # False 判断是否为全小写
52 # print("Asd".isupper()) # False 判断是否为全大写
53 # print(" ".isspace()) # True 判断是否为空格
54 # print("Asd Tre".istitle()) # True 判断是否为标题,首字母大写
55 # a = "ads"
56 # b = "456"
57 # print("".join((a,b))) # ads456 字符串拼接,效率高
58 # print("Asd Tre".upper()) # ASD TRE 变为大写
59 # print("Asd Tre".swapcase()) # asd tre 大小写反转
60 # print("Asd Tre".ljust(20,"#")) # Asd Tre############# ,靠右填充
61 # print("Asd Tre".rjust(20,"#")) # #############Asd Tre ,靠左填充
62 # print("   \tAsd Tre\n".strip()) # Asd Tre ,去掉前面或后面的空格,换行,制表符
63 # print("   \tAsd Tre\n".lstrip()) # Asd Tre ,去掉前面的空格,换行,制表符
64 # print("   \tAsd Tre\n".rstrip()) # Asd Tre ,去掉后面的空格,换行,制表符
65 # print("Asd Tre".replace("Tre","789")) # Asd 789 ,替换字符串
66 # print("Asd Tre Tre".replace("Tre","789",1)) # Asd 789 Tre ,替换字符串,替换1次
67 # print("Aesd Tre".rfind("e")) # 7 ,从右向左查找
68 # print(‘Asd Tre asdfk‘.split(‘ ‘)) # [‘Asd‘, ‘Tre‘, ‘asdfk‘] 以空格为分隔对象
69 # print(‘Asd Tre asdfk‘.split(‘s‘)) # [‘A‘, ‘d Tre a‘, ‘dfk‘] 以"s"为分隔对象
70 # print(‘Asd Tre asdfk‘.split(‘s‘,1)) # [‘A‘, ‘d Tre asdfk‘] 以"s"为分隔对象,从左边开始分割1次
71 # print(‘Asd Tre asdfk‘.rsplit(‘s‘,1)) # [‘Asd Tre a‘, ‘dfk‘] 以"s"为分隔对象,从右边开始分割1次
72 # print(‘asd tre asdfk‘.title()) # Asd Tre Asdfk ,首字母变成大写

原文地址:https://www.cnblogs.com/hlc-123/p/10926642.html

时间: 2024-07-30 04:06:23

19、python基础学习-字符串及操作的相关文章

python基础学习——字符串格式化

一.%百分号_字符串格式化 1.%s替换所有数据类型 name="I am %s my %s is %s"%("lucy","name","cc") name1="I am %s my %s is %s"%("lucy","name",[1,2]) print(name) print(name1) 结果: I am lucy my name is cc I am lu

python基础学习-字符串

字符串转换工具 单个字符串的转换可以用以下方法 1.ord(),将字符串转换成ASCII码 2.chr(),将ASCII码转换成对应的字符 stringVal = "h"print(ord(stringVal)) #将单个字符串"h"转换成->104print(chr(104)) #将ACSII码 104 ->"h" #为了生成一下个字符,可以预先将当前字符转换成整形s = '5's = chr(ord(s) + 1) #使用ord将

python基础入门---字符串常用操作

name = "qjh" print(name.capitalize())#将首字母大写输出 //Qjh #print(name.count("q") //qjh #print(name.center(50,"-"))# //-1 #print(name.endswith("s"))#以什么结尾 print(name.expandtabs(tabsize=30))# print(name.find("d")

python基础学习--字符串和文件数据处理--附代码

一. 有两行数据,存在test_data.txt: param:{"phone":"18688773467","pwd":"123456"}@url:"http://119.23.241.154:8080/futureloan/mvc/api/member/register" param:{"phone":"18688773467","pwd":&

python基础学习07(核心编程第二版)部分

# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #==================== __author__ = 'Administrator' #file与input output #文件对象 #简单说来,就是写入和读取的方式 #file(),open()2个操作都是一样的,一般推荐open() #语法 # open(name[, mode[, bufferin

python基础学习12(核心编程第二版)部分

# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #==================== __author__ = 'Administrator' #python class #面向对象编程oop思想,3个特性:封装.继承.多态,在其他方面的功能,比如重载,模拟等,也可以自定义自己需要的类 #在python中,面向对象主要2个:类和类实例 #类与实例 #类与实例有关

Python 基础学习 网络小爬虫

<span style="font-size:18px;"># # 百度贴吧图片网络小爬虫 # import re import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' imgre = re.compile(reg) imgli

python基础学习05(核心编程第二版)部分

# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #==================== __author__ = 'Administrator' #dict{键:值} #哈希 #注:字典是无顺序的,所以你懂的 #创建与赋值 dict1={} dict2={'name':'apply','avg':24,'sex':'man'} print dict1,dict2

Python基础学习(九)

Python 多线程 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的进度 程序的运行速度可能加快 在一些等待的任务实现上如用户输入.文件读写和网络收发数据等,线程就比较有用了.在这种情况下我们可以释放一些珍贵的资源如内存占用等等. 线程在执行过程中与进程还是有区别的.每个独立的线程有一个程序运行的入口.顺序执行序列和程序的出口.