1 字符串中*的使用

  *可以使字符串重复n次

 print('hello world ' * 2)  # hello world hello world 

2 索引获取字符串的字符元素

 print('hello world'[2:])  # llo world

3 in成员符

 print('el' in 'hello')  # True

4 字符串格式化

 name = 'Bob'
msg = 'my name is %s' % name
print(msg) # my name is Bob

5 字符串拼接

  采用+(建议尽量不要采用此方法,效率低,时间复杂度为平方级)

 name = 'Bob'
age = ''
msg = name + ' is ' + age + ' years old!'
print(msg) # Bob is 20 years old!

  采用join方法

    字符串是join前面的那一字符串为拼接间隔

 name = 'Bob'
age = ''
msg = name + ' is ' + age + ' years old!'
print(msg) # Bob is 20 years old!
msg_join = ' '.join([name, 'is', age, 'years old!'])
print(msg_join) # Bob is 20 years old!

6 字符串常用的内置方法

  6.1 count() 可以计算参数中的字符串在原字符串中出现的数量

 string = 'hello kitty'
count = string.count('l')
print(count) #

  6.2 center()

 string = 'title'
msg = string.center(50, '-')
print(msg) # ----------------------title-----------------------

  6.3 startswith()

    可以判断待处理的字符串是不是自己想要的字符串

 string = 'title'
print(string.startswith('t')) # True

  6.4 find()

    查找参数中的字符串并返回索引,若找不到就返回-1

 st = 'hello world'
index = st.find('w')
print(index) #
print(st.find('a')) # -1

  6.5 index()

    和find()差不多,只是在找不到的时候报错

 st = 'hello world'
index = st.index('w')
print(index) #
print(st.index('a')) # ValueError: substring not found

  6.6 lower()

    将字符串中的大写字母变成小写字母

 st = 'Hello World'
st_lower = st.lower()
print(st_lower) # hello world

  6.7 upper()

    将字符串中的小写字母变成大写字母

 st = 'Hello World'
st_upper = st.upper()
print(st_upper) # HELLO WORLD

  6.8 strip()

    将待处理的字符串的空格,换行,制表符去掉

 st = '      hello world\n'
st_strip = st.strip()
print(st_strip) # helloworld
print(len(st_strip)) #

  6.9 replace()

 st = 'hello world'
st_replace = st.replace('world', 'Bob')
print(st_replace) # hello Bob

  6.10 split()

    将字符串按照某字符进行分割,返回一个字符串元组

 st = 'my old boy'
st_split = st.split(' ')
print(st_split) # ['my', 'old', 'boy']

python基础语法之字符串的更多相关文章

  1. 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)

    """ 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...

  2. Python基础语法day_02——字符串规则

    day_02 使用方法修改字符串的大小写 将字符串首字母变成大写 >>> name = "ada lovelace" >>> print(nam ...

  3. python基础语法_字符串编码

    Python常用字符编码 http://www.cnblogs.com/schut/p/8406897.html   Python常见字符编码间的转换   在字符串写入文件时,有时会因编码问题导致无法 ...

  4. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  5. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...

  6. Python 基础语法(二)

    Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...

  7. Python 基础语法

    Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...

  8. 吾八哥学Python(四):了解Python基础语法(下)

    咱们接着上篇的语法学习,继续了解学习Python基础语法. 数据类型大体上把Python中的数据类型分为如下几类:Number(数字),String(字符串).List(列表).Dictionary( ...

  9. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

随机推荐

  1. 用Python实现简单购物车

    作业二:简单购物车# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,# 如果输入为空或其他非法输入则要求用户重新输入 shopping_list = [] w ...

  2. LVS+Heartbeat安装部署文档

    LVS+Heartbeat安装部署文档 发表回复 所需软件: ipvsadm-1.24-10.x86_64.rpmheartbeat-2.1.3-3.el5.centos.x86_64.rpmhear ...

  3. python 安装第三方包

    python环境是Anaconda3安装的,由于项目需要用到git的第三方包,但是在conda自带的环境中没有. 例如使用jieba分词库. 安装的三种方式: (1)全自动安装:`easy_insta ...

  4. QT:在其他窗口中显示QMainWindow

    问题:在QFrame中嵌入QMainWindow窗口,却无法显示QMainWindow窗口,调用QMainWindow的show()却能出现单独弹出一个QMainWindow窗口. 解决: 由于QMa ...

  5. Nowcoder Monotonic Matrix ( Lindström–Gessel–Viennot lemma 定理 )

    题目链接 题意 : 在一个 n * m 的矩阵中放置 {0, 1, 2} 这三个数字.要求 每个元素 A(i, j) <= A(i+1, j) && A(i, j) <= ...

  6. 手动升级 Confluence - 开始升级之前

    在本指南中,我们将会帮助你使用 zip / tar.gz 文件将你的 Confluence 安装实例在 Windows 或者 Linux 版本中升级到最新的版本. 升级到任何最新的版本都是免费的,如果 ...

  7. HDU 6667 Roundgod and Milk Tea

    hdu题面 Time limit 6000 ms Memory limit 131072 kB OS Windows Source 2019 Multi-University Training Con ...

  8. JavaWeb_客户端相对/绝对路径和服务器端路径

    客户端的绝对路径和相对路径 相对路径:相对与某个基准目录的路径,在同一根目录下各子目录文件之间的相互引用, 绝对路径:指目录下的绝对位置,直接到的目标位置 @charset "UTF-8&q ...

  9. VSCode安装go语言开发环境,go插件问题解决

    在安装go插件时,会自动更新很多依赖库文件,都是从Github更新下来,但是因为Github的文件中,多有应用go官网中的文件,导致,因为网络缘故,不能直接下载,导致安装失败,如下:   Instal ...

  10. What exactly is the parameter e (event) and why pass it to JavaScript functions?

    What exactly is the parameter e (event) and why pass it to JavaScript functions? 问题 Well, when I lea ...