Python圈软件测试Python基础

Python2--字符串及运算符

2020-08-24  本文已影响0人  伊洛的小屋

人生苦短,我用 Python

1. 字符串
# 伊洛Yiluo
# https://yiluotalk.com/
>>> account = 'Yiluo'
>>> my_web = 'https://yiluotalk.com/'
>>> account + ' ' + my_web
'Yiluo https://yiluotalk.com/'
>>> print('I 'm fine')
  File "<stdin>", line 1
    print('I 'm fine')
              ^
SyntaxError: invalid syntax
>>> print('I \'m fine')
I 'm fine
>>>
2.字符串索引
>>> my_web = 'https://yiluotalk.com/'
>>> my_web[0]
'h'
>>> my_web[1]
't'
>>> my_web[-1]
'/'
 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
3.字符串切片
>>> 我的公众号= '伊洛的小屋'
>>> 我的公众号[0:1]
'伊'
>>> 我的公众号[0:2]
'伊洛'
>>> 我的公众号[0:5]
'伊洛的小屋'
>>> 我的公众号[0:5:2]
'伊的屋'
# 反取
>>> 我的公众号[::-1]
'屋小的洛伊'
4. 字符串常用方法
>>> my_web = 'Yiluo,https://yiluotalk.com/'
>>> my_web.split(',')
['Yiluo', 'https://yiluotalk.com/']
>>> my_web.split(',')[0]
'Yiluo'
>>> my_web.split(',')[1]
'https://yiluotalk.com/'
>>> my_web = '   https://yiluotalk.com/   '
>>> my_web
'   https://yiluotalk.com/   '
>>> my_web.strip()
'https://yiluotalk.com/'
>>> my_web = 'https://yiluotalk.com/'
>>> my_web.strip('https:///')
'yiluotalk.com'
>>>
5. 注释
# 我是注释
'''

单引号注释
'''
print('hello, Yiluo')
"""
双引号注释
"""
6.format 格式化字符串
# 伊洛Yiluo
# https://yiluotalk.com/
>>> '{},{}'.format('Hello', 'Yiluo')
'Hello,Yiluo'
6.运算符
7.比较运算符
8.赋值运算符
9.逻辑运算符
10.成员运算符
>>> list = [1, 2, 3]
>>> 1 in list
True
>>> 5 in list
False
>>> 5 not in list
True
11.身份运算符
>>> name = 'Yiluo'
>>> id(name)
4408381744
>>> name2 = 'Tom'
>>> id(name2)
4410061168
>>> name is name2
False
>>> name is not name2
True
>>> name3 = 'Yiluo'
>>> id(name3)
4408381744
>>> name is name3
True

欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !

上一篇下一篇

猜你喜欢

热点阅读