相关文章推荐
彷徨的哑铃  ·  Global object - MDN ...·  11 月前    · 
有情有义的啄木鸟  ·  Creating Temporary ...·  1 年前    · 
风流的绿豆  ·  js中三目运算符和&& || ...·  1 年前    · 

一、访问字符串中的值

1、根据下标获取元素

# 根据下标获取字符
word = "hello"
print(word[2])     # 输出 l

2、切片式范围截取

# 方括号内输入下标范围,截取字符串;
word = "hello word"
print(word[1:5])  # 输出  ello

3、字符串连接

# 多个字符串连接用 , 或者 + 
word1 = "hello word"
word2 = "欢迎您"
print(word1, word2)  # 输出:hello word 欢迎您
print(word1 + word2)   #输出:hello word欢迎您

4、字符串格式化输出

print("My name is %s and my weight is %d Kg" % ('Lily', 55))   
# 输出:My name is Lily and my weight is 55 Kg
print("大家好,我是%3d号男嘉宾" % 5)  # 输出:大家好,我是  5号男嘉宾 ; 数字占3个位置,不够的在数字前面 空格 补齐
print("大家好,我是%03d号男嘉宾" % 5)  # 输出:大家好,我是005号男嘉宾 ; 数字占3个位置,不够的 0 补齐
print("大家好,我是%-3d号男嘉宾" % 5)  # 输出:大家好,我是5  号男嘉宾 ; 数字占3个位置,不够的在数字后面 空格 补齐

%s -- 格式化字符串;   -- 格式化整数 ;   %f 格式化浮点数字,可指定小数点后的精度 ;  %.nf  四舍五入保留小数点后n位

5、format()的使用

# {} 什么都不写,会读取后面的内容,一一对应填充
print("我叫{},今年{}岁了".format("lily", 20))  # 输出:我叫lily,今年20岁了
# {数字} 根据数字的顺序进行填入,数字从0开始
print("我叫{1},今年{0}岁了".format(20, "lily"))  # 输出:我叫lily,今年20岁了
# {变量名}
print("我叫{name},今年{age}岁了,来自{address}".format(name="lily",age=20,address="河南"))  
# 输出:我叫lily,今年20岁了,来自河南
{数字} {变量名} 可以混合使用; {} {数字}不能混合使用
# 采用字典的形式
info = {'name': 'lily', 'age': 20, 'address': '河南', 'height': 166}
print("我叫{name},今年{age}岁了,来自{address},身高{height}".format(**info))

二、字符串的内建函数

1、capitalize() 把字符串的第一个字符大写

word1 = "hello word"
print(word1.capitalize())  # 输出:Hello word

2、startswith("ww") 字符串是否以ww开头;  endswith("ww") 字符串是否以ww结尾;

word1 = "helloword.txt"
print(word1.endswith('.txt'))  # 输出:True
print(word1.startswith("He"))  # 输出:False

3、isalpha()   字符串是否都是字母

4、isalnum()   所有字符都是字母或数字则返回 True

5、isdigit()   只包含数字则返回 True

6、lower()   所有大写字符转为小写

小写字母转为大写

7、replace(str1, str2,    str1 替换成 str2,如果 num 指定,则替换不超过 num 次

8、partition(str)    str出现的第一个位置起,把字符串string 分成一个3元素的元组(string_pre_str,str,string_post_str),

# 如果 string 中不包含str 则 string_pre_str == string

9、rpartition(str)   类似于 partition()函数,不过是从右边开始查找

10、split(str="", num=string.count(str))   以 str 为分隔符切片 string,如果 num 有指定值,则仅分隔 num+1 个子字符串

word1 = "helloword.txt"
    print(word1.split(".", 2))   # 以 . 分割,只分割2次
    # 输出:['helloword', 'txt']

11、len(str) 获取字符串的长度

12、 find(str, beg=0, end=len(string))  检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1

word1 = "helloword.txt"
print(word1.find("txt"))  # 包含时 返回索引值
print(word1.find("aaa"))  # 不包含 返回-1

python 查找dataframe python 查找word中的字符串_3d

13、in 和 not in : 判断字符串是否存在str

word1 = "helloword.txt"
if 'x' in word1:
    print("存在")  # 包含时 返回索引值
else:
    print("不存在")  # 不包含 返回-1

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

word1 = "helloword.txt"
if '22' not in word1:
    print("不存在")  # 包含时 返回索引值
else:
    print("存在")  # 不包含 返回-1




sql server点击哪里可以找到新建查询保存的东西 sql2000新建查询在哪里

--(在命令行中输入'isqlw'命令可打开查询分析器 ) --(如果要执行多条命令中的一条,鼠标选定后再执行) create database sales --创建一个名为sales的数据库 name='sales_data', filename='d:/sales_data.mdf', size=10, maxsize=50,

python输入5如何输出05 python输入123456输出654321

print()函数的3种用法 第一种,仅用于输出字符串或单个变量  print(<待输出字符串或变量>)>>> print("Pyhon 世界") Pyhon 世界 >>> print(10.01) 10.01 >>> print([1,2,3,4]) [1, 2, 3, 4] >>> print(["a