ps:19年毕业后,一直从事大数据相关的工作,所以决定开始学习python,在此记录从零开始学习python的点点滴滴,愿你我一起坚持,相互学习,共同进步!
个人觉得需要关注的点都在注释里面哈,欢迎补充拓展。
一、字符串
字符串就是一系列的字符。
在python中,字符串需要使用引号(单引号 or 双引号)括起来,例如:'123','abc','Just and only one'....
1、修改字符串大小写的方法:
title、capitalize、upper、lower#!/usr/bin/python3
import sys;
x = 'welcome To';
y = 'MY HOME'
#(1)sys.stdout.write输出不会换行
sys.stdout.write(x + '\n')
#(2) 方法title是以首字母大写的方式,显示每个单词。
print(x.title())
#(3)方法capitalize是首单词首字母大写的方式,显示字符串。
print(y.capitalize())
#(4)方法upper是字符串中所有字符均大写。
print(x.upper())
#(5)方法lower是字符串中所有字符均小写。
print(y.lower())
#(6)字符串中使用变量:f字符串:format
print(f"{x} {y}") #字符串中两个变量间的空格会生效
print(f"{x}{y}")
print(f"hello! {x} {y} 1")
print(f"Hellow! {x.title()} {y.title()} 2")
# 把f字符串赋予变量
message = f"Hellow! {x.title()} {y.title()} 3"
print(message)执行结果:
welcome To
Welcome To
My home
WELCOME TO
my home
welcome To MY HOME
welcome ToMY HOME
hello! welcome To MY HOME 1
Hellow! Welcome To My Home 2
Hellow! Welcome To My Home 3
Process finished with exit code 0
2、制表符&换行符&去除首尾空格(剥除函数)
strip、lstrip、rstrip#制表符\t 换行符\n
x = '\thello!\n\twelcome\n\tTo\n\tMYHOME 1'
y = ' hello! welcome to my home '
print(x)
# 方法strip可以去除字符串首尾空格
print(y.strip())
#lstrip 删除字符串开头空格
print(y.lstrip())
#rstrip 删除字符串末尾空格
print(y.rstrip())
#去除空格方法常用于存储用户输入信息前,对输入内容全部进行小写处理 hello!
welcome
To
MYHOME 1
hello! welcome to my home
hello! welcome to my home
hello! welcome to my home
Process finished with exit code 0去除空格方法在python自带编译工具“IDLE”中运行效果更明显。
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
>>>
>>>
>>>
>>> x = ' abc '
>>> x
' abc '
>>>
>>> x.strip()
'abc'
>>>
>>> x.lstrip()
'abc '
>>>
>>> x.rstrip()
' abc'
>>>
>>> Ps:字符串内容为: 佛说:‘色即是空,空即是色’。 该如何用表示呢?
Buddhism said:'Color is emptiness, emptiness is color'.
#字符串中含有单引号时,字符串使用双引号括起来
string = "Buddhism said:'Color is emptiness, emptiness is color'."3、数(浮点数、整数、常量...)
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 9+9
18
>>> 9-9
0
>>> 9/9
1.0
>>> 9*9
81
>>>
>>> 1.2+1.2
2.4
>>> 1.2-1.2
0.0
>>> 1.2*1.2
1.44
>>> 1.2/1.2
1.0
>>>
>>> 1+1.2
2.2
>>> 2-1.2
0.8
>>> 2*1.2
2.4
>>> 8/1.2
6.666666666666667
>>> 在赋予变量的数较大时,可以使用下划线,方便阅读查看,并且不影响代码编译。(适用于整数、浮点数)
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
>>> x = 1_000_000_00
>>> x
100000000
>>>
>>> x = 1.0_123_456
>>> x
1.0123456
>>> 在python中没有常量类型的定义,但是大家通常会将字母全部大写的变量视为常量。
NORMAL_A = 1004、字符串截取
字符串是有顺序的,从左到右,由 0 开始编码;从右到左,由 -1 开始编码。
str = 'abcdefghijk'
print(str) # 输出字符串
print(str[0:-1]) # 输出第一个到倒数第二个的所有字符
print(str[0]) # 输出字符串第一个字符
print(str[2:5]) # 输出从第三个开始到第五个的字符
print(str[2:]) # 输出从第三个开始后的所有字符
print(str[1:5:2]) # 输出从第二个开始到第五个且每隔两个的字符
print(str * 2) # 输出字符串两次
print(str + '你好') # 连接字符串
print('hello\npython') # 使用反斜杠(\)+n转义特殊字符
print(r'hello\python') # 在字符串前面添加一个 r,表示原始字符串,不会发生转义5、字符串宽度填充
center
str = 'abc'
#方法center(长度,填充字符),并且原字符居中
#字符串x长度为7,空余部分用字符$填充
x = str.center(7,'$')
print(f"{x}")$$abc$$
Process finished with exit code 0
6、统计字符出现次数
count
str = 'abcabcabc'
#方法count()可统计字符串中某字符出现的次数
y = str.count("a")
#字符串是有顺序的,可以查找某长度内字符出现次数
x = str.count("a",0,4)
print(f"{y}")
print(f"{x}")3
2
Process finished with exit code 0
7、字符串编码与解码
encode、decode
str1 = '我爱我的祖国'
str2 = 'I love my country'
#方法encode("编码格式"),实现编码格式转换
print("utf-8编码",str1.encode("utf-8"))
print("gbk编码",str1.encode("gbk"))
print("utf-8编码",str2.encode("utf-8"))
print("gbk编码",str2.encode("gbk"))
print("--------------------------------------")
x = str1.encode("utf-8")
y = str1.encode("gbk")
#方法decode("编码格式"),实现解码
print(x)
print("utf-8解码",x.decode("utf-8"))
print("gbk",y.decode("gbk"))utf-8编码 b'\xe6\x88\x91\xe7\x88\xb1\xe6\x88\x91\xe7\x9a\x84\xe7\xa5\x96\xe5\x9b\xbd'
gbk编码 b'\xce\xd2\xb0\xae\xce\xd2\xb5\xc4\xd7\xe6\xb9\xfa'
utf-8编码 b'I love my country'
gbk编码 b'I love my country'
--------------------------------------
b'\xe6\x88\x91\xe7\x88\xb1\xe6\x88\x91\xe7\x9a\x84\xe7\xa5\x96\xe5\x9b\xbd'
utf-8解码 我爱我的祖国
gbk 我爱我的祖国
Process finished with exit code 0**翻转字符串:
#变量[头下标:尾下标:步长],如果第三个参数为负数表示逆向读取。
def reverseWords(input):
# 通过空格将字符串分隔符,把各个单词分隔为列表
inputWords = input.split(" ")
# 翻转字符串
# 假设列表 list = [1,2,3,4],
# list[0]=1, list[1]=2 ,而 -1 表示最后一个元素 list[-1]=4 ( 与 list[3]=4 一样)
# inputWords[-1::-1] 有三个参数
# 第一个参数 -1 表示最后一个元素
# 第二个参数为空,表示移动到列表末尾
# 第三个参数为步长,-1 表示逆向
inputWords = inputWords[-1::-1]
# 重新组合字符串
output = ' '.join(inputWords)
return output
if __name__ == "__main__":
input = 'I like python'
rw = reverseWords(input)
print(rw)python like I
Process finished with exit code 0版权声明:本文为fly_wang_sun原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。