2021-03-28字符串

@Time :2021/3/28 15:03

@Author: 小被几

@File :demo01.py

"""
换行:Ctrl+shift+enter
整理代码:Ctrl+Alt+l
看源码:Ctrl+鼠标点击

一、字符串切片
1、倒序切片
# 推荐写法
正序:
str3=str[1:5:1]

# 面试使用
str4=str[-2:-5:-1]
         1    2   3   4    5   6

正序: 0 1 2 3 4 5
逆序: -6 -5 -4 -3 -2 -1

2、字符串重复输出
print("#"*20) 输出20个#符号

二、字符串转义(使用于路径处理)

1、 转义字符
\n 换行符号

     print("hello\npython")            ---hello
                                     python
    print(r"hello\npython")           ---hello\npython

\ 关闭转义

    print("hello\\npython")           ---hello\npython
    print("hello\n\python")           ---hello
                                    \python

  #   print("\\")                       ---\

r:路径处理

    print(r"jels\n\\ss\n\\")          ---jels\n\\ss\n\\

三、字符串方法
1、适用于验证码不区分大小写
str.upper()---全部大写
str.lower---全部小写
str.capitalize()---首字母大写
str.swapcase()---大小写互换

换行:Ctrl+shift+enter

大写:
#str="python"
#new_str1=str.upper()
#print(new_str1)
小写:
# str1="PYTHON"
# new_str2=str1.lower()
# print(new_str2)
单词首字母大写:
# str3="python"
# new_str4=str3.capitalize()
# print(new_str4)
单词大小写互换:
# str4="pYtHoN"
# new_str5=str4.swapcase()
# print(new_str5)

2、统计相关

count():统计字符在该字符串的出现次数(区分大小写)
    sub:需要统计的字符
    start:统计范围的开始索引
    end:统计范围的结束索引
    返回:sub在字符串中出现的次数
find():查找字符在该字符串的索引位置
    sub:需要统计的字符
    start:统计范围的开始索引
    end:统计范围的结束索引
    默认只查询第一次找到的字符,返回索引值

          # str="hello python"
          # times=str.count("h",0,3)
          # print(times)      ---1


          # str="hello python"
          # index=str.find("h")
          # print(index)

3、拆分&替换
split():字符串拆分,默认空格键拆分,可指定字符及次数
拆分过程会丢弃指定字符
maxsplit参数控制拆分次数,默认(-1)无限次,负数表示不限次,0表示不拆分
# str="hello python"
# new_str1=str.split()
# print(new_str1) ---['hello', 'python']

        # str="hello python"
        # new_str1=str.split("h")
        # print(new_str1)           ---['', 'ello pyt', 'on']

        # str="hello python"
        # new_str1=str.split("o",1)
        # print(new_str1)           ---['hell', ' python']

replce():
第一个参数:需要替换的字符
第二个参数:替换后的字符
第三个参数:替换次数
返回替换后的字符串
str="hello python"
new_str1=str.replace("o","8",1)
print(new_str1) ---hell8 python

4、字符串连接:join方法

转换为h-e-l-l-o-p-y-t-h-o-n

        str="hello python"
        new_str1="-".join(str)
        print(new_str1)

5、字符串的格式化 % {}
%:占位符
%s:字符串占位符 如果不是字符串 会发生强制转换
test="this is test %s"%("py39") ---this is test py39 不加括号也行
%d:int类型占位符
test="this is num %d"%(32) ---this is num 32

1、以下是正确的字符串(D)

A ‘abc”ab” B ‘abc”ab’ C “abc”ab” D “abc\”ab”

2、“ab”+”c”*2 结果是:(c)

A abc2 B abcabc C abcc D ababcc

3、用python实现"hello world hello python" 变成 python hello world hello
替换:
test="hello world hello python"
test2=test.replace("hello world hello python","python hello world hello")
print(test2)

拆分:
test="hello world hello python"
test2=test.split()
test3=" ".join(test2[::-1])
print(test3)

        test="hello world hello python"
        test2=test.split()
        test2.reverse()
        test3=" ".join(test2)
        print(test3)

4、将"hello world"转为首字母大写"HELLO WORLD"

    test="hello world"
    test2=test.upper()
    print(test2)          ---HELLO WORLD


    test="hello world"
    test2=test.title()
    print(test2)          ---Hello World



    test="hello world"
    test2=test.capitalize()
    print(test2)            ---Hello world

5、将字符串"I Love Java" 变成"I Love Python"
test="I Love Java"
test2=test.replace("Java","Python")
print(test2)

"""

你可能感兴趣的:(2021-03-28字符串)