一、重点掌握的6种字符串方法:

1、join命令

功能:用于合并,将字符串中的每一个元素按照指定分隔符进行拼接

程序举例:

seq = ['1','2','3','4']
sep = '+'
v = sep.join(seq)
print(v)

test = "学习要思考"
t = '***'
v = t.join(test)
print(v)

2、split命令

功能:与join相反,将字符串拆分为序列

test = '1+2+3+4+5'
v = test.split('+')
print(v)

test = '/usr/bin/env'
v = test.split('/')
print(v)

3、find命令

功能:在字符串中寻找子串。如果找到,就返回子串的第一个字符索引,否则返回-1.

test1 = 'with a moo-moo here, and a moo-moo there'
v1 = test1.find('moo')
print(v1) test2 = "Monty Python's Flying Circus"
v2 = test2.find('Monty')
v3 = test2.find('Python')
v4 = test2.find('Flying')
v5 = test2.find('Zirquss')
print(v2)
print(v3)
print(v4)
print(v5)

可以指定搜索起点和终点

test = '### Get rich now!!! ###'
v = test.find('###', 1)
v1 = test.find('!!!')
v2 = test.find('!!!', 0, 16)
print(v)
print(v1)
print(v2)

4、strip命令:

功能:将字符串开头和结尾的空白(不包括中间的空白)删除,或者删除指定字符

test = '*** smart * fast * strong!!! ***'
v = test.strip(' *!')
print(v)

names = ['gumby', 'smith', 'jones']
name = 'gumby '
if name in names:
print('Found it')
else:
print('Not exist')
if name.strip() in names:
print('Found it')

5、upper命令和lower命令:

test = "aLex"
v1 = test.upper()
v2 = test.lower()
print(v1)
print(v2)

二、字符串常见四种应用:

1、索引,下标 获取字符串中的某个字符

test = "alex"
v = test[2]
print(v)

2、切片,索引范围  0 =<  <1

test = "alex"
v = test[0:2]
print(v)

3、len获取当前字符串中由几个字符组成

test = "alex"
v = len(test)
print(v)

test = "圣诞节爱范娜"
index = 0
while index < len(test):
v = test[index]
print(v)
index += 1
print('=======')

4、for循环:(非常重要)
for 变量名 in 字符串:
      变量名
for循环,索引,切片

test = "圣诞节爱范娜"
for item in test:
print(item)

range命令:帮助创建连续数字,,通过设置步长来指定不连续数字

v = range(0,100)
for item in v:
print(item)

v = range(0,10,2)
for item in v:
print(item)

************例题:将文字,对应的索引打印出来**************

test = input(">>>")
v = range(0,len(test))
for item in v:
print(item,test[item])

*********************

注意:

********************************************
字符串一旦创建,不可修改
一旦修改或者拼接,都会造成重新生成字符串
********************************************

python learning 字符串方法的更多相关文章

  1. python拼接字符串方法汇总

    python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!' print(s) 输出结果:Hello World! 这种方式最常用. ...

  2. Python常见字符串方法函数

    1.大小写转换 S.lower() S.upper() 前者将S字符串中所有大写字母转为小写,后者相反 S.title() S.capitalize() 前者返回S字符串中所有单词首字母大写且其他字母 ...

  3. python之字符串方法upper/lower

    1.描述: upper():用于将字符串全部转换为大写字母 lower():用于将字符串全部转换为小写字母 2.语法 str.upper() str.lower() 3.返回值 upper()或low ...

  4. Python之字符串方法

    def capitalize(self): # 第一个字符变大写 def center(self, width, fillchar=None): # 内容居中,两端可指定内容填充 def count( ...

  5. python,字符串方法

    1.capitalize() 首字母大写 text = "hello word" text2 = text.capitalize() print(text2) 2.1.casefo ...

  6. python中字符串方法总结

    定义一个空字符串: a=' '; s.strip() #去空格 s.upper()#全部转换成大写: s.lower()# 全部转换成小写: s.isdigit()#判断字符串是否只有数字组成:返回t ...

  7. Python中的字符串方法

    Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...

  8. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  9. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息.        ...

随机推荐

  1. windows环境隐藏命令行窗口运行Flask项目

    Linux下可以使用nohub来使Flask项目在后台运行,而windows环境下没有nohub命令,如何让Flask项目在windows中在后台运行而不显示命令行窗口呢? 1.写一个.bat脚本来启 ...

  2. torch.linspace,unsqueeze()以及squeeze()函数

    1.torch.linspace(start,end,steps=100,dtype) 作用是返回一个一维的tensor(张量),其中dtype是返回的数据类型. import torch print ...

  3. python图片转为base64

    # -*- coding: utf-8 -*- import base64 with open("/home/chaowei/1.png","rb") as f ...

  4. wireshark如何抓取localhost包

    1.首先安装NPCAP 下载地址:https://nmap.org/download.html 安装时,记得勾上最后一个选项:wincap模式 安装完成后,一定要重启系统 2.安装wireshark ...

  5. Python-Django-Ajax进阶2

    -forms组件的渲染错误信息 在模板中:<span>{{ foo.errors.0 }}</span> -forms使用bootstrap样式 widget=widgets. ...

  6. Android开发之字体设置

    默认字体 Android SDK自带了四种字体:"normal"“monospace",“sans”, “serif”,如下:   字体 看这四兄弟长的还是蛮像,我是看不 ...

  7. CSS使用小记

    1. 省略显示 max-width: 200px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; 固定宽度,nowra ...

  8. Javascript我学之五对象的创建与使用

    本文是金旭亮老师网易云课堂的课程笔记,记录下来,以供备忘. 对象的创建 JavaScript对象有两种类型   1).Native:在ECMAScript标准中定义和描述,包括JavaScript内置 ...

  9. [原创]c# 岛2 小辅助~~~ 钓鱼 连击

  10. 新世界主机_XenServer7.0都有哪些优势?

    新世界主机VPS全部都采用了Xen硬件虚拟化技术,每个用户都能够独享资源,一键就可以创建和重装VPS,每个VPS都拥有足够的带宽,保证顺畅运行(http://m.0830mn.com). 新世界主机使 ...