python中字符串的基本常识。

字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。

创建字符串很简单,只要为变量分配一个值即可。例如:

info = '今天是星期四 Hello World'

Python不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。

Python访问子字符串,可以使用方括号来截取字符串。

# 值1:开始位置 (包括该位置)
# 值2:结束位置  (不包括该位置)
print(info[3 : 5])

如果当输入的范围超出边界的时候,就会直接截取字符串结束的部分。

print(info[100])和print(info[16])输出结果是一样的。

# 获取从指定位置到结束位置的内容
print(info[3:])
# 反序截取字符串
print(info[:-3])
# 相当于直接获取info的整个内容

print(info[:])

#相当于取前三个字符串

print(info[:3])

---------------find---------------

content = '张三李四王五'

result = content.find('李四')
在find寻找字符串的时候,字符串存在会输出0,不存在会输出-1
print(result)
# 不存在 -1 存在是0
if result == -1 :
    print('不存在')
else :

    print('存在')

# -------------------------index-----------------------
info = '好好学习,天天向上'
# 在整个info范围内找
result = info.index('学习')
# 在编号5与7之间找
# result = info.index('学习',5,7)
print(result)


# ---------------------count--------------------
info = 'hello world'
#  获取指定子元素的个数
result = info.count('l')

print(result)        

#输出结果就是3


info = '程序员,设计师,工程师'
# replate 替换
# 值1:旧值
# 值2:新值
info = info.replace(',','/')
print(info)

# --------------------------------split--------------------------
url = 'http://www.baidu.com/image.jpg'

print(url[21:])

运行结果为:

# split 分割
result = url.split('/')
print(result)

print(result[-1])

运行结果为:

首字母的大小写问题:

info = 'hello world'
# 首字母大写
print(info.capitalize())
# 全部首字母大写

print(info.title())

------------------------------------

info = 'Hello World'
# 全部变成小写
info = info.lower()
print(info)
# 全部变成大写
info = info.upper()
print(info)

------------------------------------------------

url = 'taobao.com'
# 如果url不是以XXXX开头
if not url.startswith('https://www.'):
    url = 'https://www.' + url

print(url)

这就是为什么我们在输入网站的时候不用大前面的http://也可以进入目标网站的原因。

-----------------------------------------------------------------------------------------

message = '我要好好学习,天天向上'
# 指定规则
s = str.maketrans('学习','奋斗')
# translate翻译  根据规则进行翻译

print(message.translate(s))

运行结果为:

将一串字符串里面的数字全部挑出来,形成新的列表。

------------------------------------------------------------------

'!@#$%asdsfghfgj32324356576#&#$%^&*()_dsfgh'
str = 'asdaSfawf5645646csfcc45634fd,./,;566654Fdcf'
index = 0
str1 = []
while index<len(str):
    if str[index] >="0" and str[index]<="9":
        str1 += str[index]
    index += 1
print(str1)

----------------------------------------------------------------

a = '123567'
for i in '-123abc567':
    if i.isdigit():  #'1' '2'
        a += i
a = int(a)   #123567
print(a)

从‘-123asc456’这个字符串中挑选出数字输出。



index = input('请随便输入')
for index in index :
     if index.isdigit():
         index = int(index)

         print(index,end='')

将输入的字符串中的数字挑出来。

code = "selectLine.style.transform = 'rotate(' +  (selectLine.angel + 360 / allNames.length ) + 'deg)"
code = code.replace("(",".")        #用‘.’把‘(’替换掉

code = code.split(".")                   #用‘.’把字符串分割开

for x in code:                                            

    if x.startswith("s"):
         print(x)

关于字符串还有很多的知识。

猜你喜欢

转载自blog.csdn.net/qq_39138295/article/details/80847787