python a basic data type (String)

String (str)

String, for storing some small amount of data, as long as it is a quoted string, each individual letter characters is a string element Pyhton them.

On the first day when we met under a simple string, and today we can really come to know about this makes you both happy and gifted string

Let's recall next string is not to be Math

String plus

a = '世界'
b = '你好'
print(a + b)
结果:
世界你好

By just our tests found that the string can also be an addition operation, the sum actually have a string of professional terminology --- string concatenation; adding the rule is to be summed are strings

The string can not be subtraction and division operations, through these words we can know if a string can be multiply operation, we immediately look:

Take the string

a = '坚强'
print(a * 8)
结果:
坚强坚强坚强坚强坚强坚强坚强坚强

Multiplying the plurality of strings is a string stitched together, multiplication rules: Multiplying the strings and only

We recall the complete contents of the first day of the characters, then take a look at what we talk about today is ??

Detailed string method

ALL CAPS

name = 'alex'
new_name = name.upper()
print(new_name)
# 把这个字符串全部变成大写

All lowercase

name = 'ALEX'
new_name = name.lower()
print(new_name)
# 把这个字符串全部变成小写

Scenario:

# 字符串大小写做验证码
y_z_m = 'O98k'
y_z_m_input = input("请输入验证码(O98k)")
user = input('请输入账号:')
pwd = input('请输入密码:')
if y_z_m == y_z_m_input:
    if user == 'alex' and pwd == '8520':
        print('登陆成功!')
    else:
        print('登录失败')
else:
    print('验证码错误!')

What begins with  

name = 'alex'
new_name = name.startswith('a')
if new_name:
    print('是以a开头')
# 判断这是不是字符串是不是以a开头的

name = 'alex'
new_name = name.startswith('e',2,5)
if new_name:
    print('是以e开头')

# 我们可以指定从哪个位置开始哪个位置结束

To what end  

name = 'alex'
new_name = name.endswith('x')
if new_name:
    print('是以x结尾')
# 判断这是不是字符串是不是以x结尾的

name = 'alex'
new_name = name.endswith('x',2,5)
if new_name:
    print('是以x结尾')
# 判断这是不是字符串是不是以x结尾的

The number of statistics appear

name = 'alexdasx'
new_name = name.count('a')
print(new_name)
# 统计name这个字符串中a出现的次数

String replacement  

name = 'alexdasx'

new_name = name.replace('sx','sb',1)

print(new_name)

# 替换字符串中的内容以外 我们可以指定要替换的次数

Newline spaces on both sides of the head and tail removed / tab  

name = ' alexdasx '
new_name = name.strip() # 默认去除的头尾两端的空格,换行符,制表符 也可以自己指定
print(new_name)
# 去除头尾俩遍的空格和换行符

Split

name = 'alexdasx'
new_name = name.split("x")  # 默认以空格,换行符,制表符分隔
print(new_name)
# 也可以通过x将字符串name进行切割

name = 'alexdasx'
new_name = name.split("x",maxsplit=1)  # 默认以空格,换行符,制表符分隔
print(new_name)
# 切割后的内容是一个列表

series is

Judgment is not decimal number, returns a Boolean value

name = 'alexdasx'
new_name = name.isdecimal()
print(new_name)

# 判断是不是十进制的数字,返回结果是布尔值

Judgment is not numbers and letters as well as Chinese, returns a Boolean value

name = 'alex7dasx'
new_name = name.isalnum()
print(new_name)

# 判断是不是数字和字母以及中文,返回结果是布尔值

Letters and characters is not judged, it returns a Boolean value

name = 'alexdasx'
new_name = name.isalpha()
print(new_name)

# 判断是不是纯字母和汉字,返回的是布尔值

Get the length of

Currently we write content is still relatively small, if there is a very, very long string, one by one if the number is not tired of it, Python provides a way to give us is to get the length.

Length for examples:

name = "Meet"
print(len(name))
结果:
4 
这里需要大家清楚一点的就是,len是一个公共的方法,不是字符串的方法只有用字符串.的方式使用的才是字符串的方法

We are now able to easily get to the length, so now I have a string name = "Meet", use a while loop to get each element of the string and print, renderings into the next:

img

String Methods

upper()#将字符串全部大写
lower()#将字符串全部小写
startswith()#判断以什么开头,可以指定范围
endswith()#判断以什么结尾,可以指定范围
strip()#去除空格换行制表符,可以指定去除的字符
replace()#替换,可以指定次数
isalnum()#判断是不是数字字母汉字组成
isdigit()#判断是不是阿拉伯数字
isalpha()#判断是不是字母和汉字
isdecimal()#判断是不是十进制
len()#获取字符串的长度
split()#以空格换行制表符分割,可以指定分割符和次数
rsplit()
#从右边开始分割
count()#获取字符出现的次数
find()#查找字符在字符串中出现的次数,可以指定开始与结束,有返回索引,否则返回-1
index()#与find一样,但是如果找不到就报错
capitalize()#把字符串的首字母大写
'_'.join(s)#使用指定的字符吧字符串的每个字符分割
casefold()#与lower一样,但是lower只对ascii由效
format()#格式化输出,用{}表示
center()#返回字符串居中,空格填充的指定长度的字符串
decode()#以指定的编码格式进行解码
encode()#以指定格式进行编码,二进制
expandtabs()#将字符串中的tab键转换成空格,默认为8个
isidentifier()#判断是不是有效的puthon标识符
isspace()#判断字符串中是不是值只包含空格
isnumeric()#判读字符串是不是只包含数字字符
isprinttable()#判断是不是都是可打印的字符或字符串,可以用来查看是不是包含转义符
ljust()#与center相似,字符串左对齐,空格补充至指定长度
rjust()#与ljust相反
partition()#与split相似,但是分割后分割字符保留,返回三元字符串
splitlines()#以换行符进行分割,返回分割后的列表
swapcase()#对字符串中的大小写进行反转

String sections

slice

What is it sliced ​​Let's look at an example?:

name = 'meet'
# 索引  0123
print(name[0:3])
结果:
mee
#**********
name = "今天是个好日子"
      # 0 1 2 3 4 5 6
      #-7-6-5-4-3-2-1
a = name[0]
b = name[1]
print(a+b)
切片
print(name[0:2]) # 顾头不顾尾  name[起始位置:终止位置]
print(name[:]) # 某个位置不指定的时候默认取最后或最前
print(name[2:5])
print(name[-2:-5])
print(name[-2:-5:-1]) # [起始位置:终止位置:步长] 步长默认为1

[The first is the start position: second position is terminated] intermediate must semicolon, such wording is to start from index 0 to index acquired end 3  

This is not the result we wanted and a bit out ah, we certainly believe that the acquired content is meet, but why is it mee, because of the termination of the index is not included in the acquisition of the contents of this section

Think we buy meat at the time, there is a whole piece of meat, whole piece of meat on the back a little fat, and if not let the boss do not want to cut off, we'll take part of the front ah This God operating in your life is sliced

I know you just took these things, the use is not very familiar with, especially in this part of the slice, quietly tell you a tip, when you see the next time slice end position of this number minus one on ok friends

Steps

? What steps is it to look at an example:

name = 'meet'

# 索引  0123

print(name[0:3:1])

结果:

mee

image-20190622205656580

I found written in parentheses last position of a thing, nothing changes, is not changed, because we do not write when he default is 1, we change the numbers to look at

name = 'meet'

# 索引  0123

print(name[0:3:2])

结果:

me

This is what happens then? Think I began to tell everybody in parentheses first parameter is the starting position, and the second parameter is the end position, the third parameter to tell you now is the step size (each a few steps away)

image-20190622205724596

When the step size is set to 2, when we only need to add a starting position 0 step 2, then 2 will result in the index to find 2, 2 plus 2 is 4 steps, when you want to find the index 4 is found to terminate the index is 3,

All will not find it. The end result is me.

Guess you like

Origin www.cnblogs.com/luckinlee/p/11619785.html