分享

python每天一道面试题14

 禁忌石 2023-01-09 发布于浙江

Python中str字符串?

字符串是不可变的序列数据类型,不能直接修改字符串本身,和数字类型一样!Python3全面支持Unicode编码,所有的字符串都是Unicode字符串。

  • 转义字符
# 字符串前加 u、r、b# u'中文字符组成的字符串'# 作用:以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。# r'\n\n\n\n”  # 表示一个普通生字符串 \n\n\n\n,而不表示换行# 作用:去掉反斜杠的转义机制,常用于正则表达式,对应着re模块。# b'Hello World’ # 表示这是一个 bytes 对象# 作用:b' '前缀表示:后面字符串是bytes 类型。在网络编程中,服务器和浏览器只认bytes 类型数据。# tip:在Python3 中,bytes 和 str 的互相转换方式是# str.encode('utf-8')和bytes.decode('utf-8')
  • 判断字符串是不是数字
def test_string_whether_numeric():    st = 'sd234'    try:        num = float(st)    except (ValueError, TypeError):        print('not numeric')
  • count使用
def test_count(): s = 'The quick brown fox jumps over the lazy dog.' print('the occurrence times of character %s is %s' % ('e', s.count('e')))test_count()# the occurrence times of character e is 3
  • 两个相同的字符串指向同一内存地址
st1 = 'dong'st2 = 'dong'print('st1的内存地址==%s\nst2的内存地址==%s' % (hex(id(st1)), hex(id(st2))))# st1的内存地址==0x21b3f5dc4f0# st2的内存地址==0x21b3f5dc4f0
  • 字符串中添加尾随和前导零
def test_add_trailing_and_leading_zeroes_to_a_string(): st = 'dgfr45sfry4' print('origin string---%s, len(st)---%s' % (st, len(st))) st1 = st.ljust(15, '0') print('add trailing zeroes---', st1) st2 = st.ljust(15, '*') print('add trailing *---', st2) st3 = st.rjust(15, '0') print('add leading zeroes---', st3) st4 = st.rjust(15, '*') print('add leading zeroes---', st4) test_add_trailing_and_leading_zeroes_to_a_string()# origin string---dgfr45sfry4, len(st)---11# add trailing zeroes--- dgfr45sfry40000# add trailing *--- dgfr45sfry4****# add leading zeroes--- 0000dgfr45sfry4# add leading zeroes--- ****dgfr45sfry4
  • zfill使用
def test_combination_3_digit():    nums = []    for x in range(15):        num = str(x).zfill(3)        nums.append(num)    return numsprint(test_combination_3_digit())# ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012', '013', '014']
  • replace方法
st = string1.replace(old, new[, max]) # 会生成一个新对象返回,原来的字符串string1还是原来的值
  • split方法
def get_last_part_string(st):    print(st.split('/'))    print(st.rsplit('/'))    print(st.split('/', 1))    print(st.split('/', 2))    print(st.split('/', 3))    return st.rsplit('/', 1)[0], st.rsplit('-', 1)[0]# split(' ')解决不了单词间多空格的问题,s.split()可以解决# s = 'a good   example'# s.split(' ')# ['a', 'good', '', '', 'example']# s.split()# ['a', 'good', 'example']# print(get_last_part_string('https://www./python-exercises/string'))# output:# ['https:', '', 'www.', 'python-exercises', 'string']# ['https:', '', 'www.', 'python-exercises', 'string']# ['https:', '/www./python-exercises/string']# ['https:', '', 'www./python-exercises/string']# ['https:', '', 'www.', 'python-exercises/string']# ('https://www./python-exercises', 'https://www./python')
  • upper()与lower()
st.upper() # 字符串全大写st.lower() # 字符串全小写
  • startswith()
Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。语法:startswith()方法语法:str.startswith(str, beg=0,end=len(string));参数:str -- 检测的字符串。strbeg -- 可选参数用于设置字符串检测的起始位置。strend -- 可选参数用于设置字符串检测的结束位置。str = 'this is string example....wow!!!';print str.startswith( 'this' );print str.startswith( 'is', 2, 4 );print str.startswith( 'this', 2, 4 );# True# True# False

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多