03_字符串用法详解

# 1, 字符串的格式化问题
# 我叫xxx,我住xxx,我今年xx岁,我喜欢做xxx
# name = input("请输入你的名字:")
# addr = input("请输入你的住址:")

# s2 = f"我叫{name},我家住{addr}"
# print(s2)

# 2,索引和切片
# 索引:按照位置提取元素
# s = "我叫周杰伦,你呢,你叫周润发吗?"
# print(s[0:5])
# print(s[:5])
# print(s[6:])
# print(s[:])
# print(s[-3:-1])
# print(s[-3:])

# s = "我爱你"
# #可以给切片添加步长来控制切片的方向
# print(s[::-1])

#3, 字符串常用操作

#如何忽略大小写来进行判断
# verify_code = "xAd1"
# user_input = input(f"请输入验证码({verify_code}):")
# if verify_code.upper() == user_input.upper():
#     print("验证码正确")
# else:
#     print("验证码错误")


# 4,替换和切割
# strip()
# s = "   你好,   我叫  周杰 伦  "
# s1 = s.strip()
# print(s1)

# 案例
# username = input("请输入用户名:").strip()
# passwd = input("请输入密码:").strip()
# if username == "admin":
#     if passwd == "123456":
#         print("登录成功")
#     else:
#         print("登录失败")
# else:
#     print("登录失败")


# replace(old,new)字符串替换

# s = "你好啊,我叫赛琳娜"
# s1 = s.replace("赛琳娜", "周杰伦")
# print(s1)

# s = "he llo i am a good m an"
# s1 = s.replace(" ", "")
# print(s1)

# split(用什么切割)字符串切割,用什么切就会损失掉谁
# a = "python_java_c#_c++_JavaScript"
# # lst = a.split("_")
# # print(lst)
# lst = a.split("_JavaScript")
# print(lst)

#总结:replace(),split(),strip()  ==>记住


# 5,查找和判断

# startwith(), isdigit(), in, not in, find

# 6, 补充和总结
# s = "hello"
# print(len(s))  # length  长度

# join()
# a = "python_java_c#_c++_JavaScript"
# lst = a.split("_")
# print(lst)

# lst = ['python', 'java', 'c#', 'c++', 'JavaScript']
# #用_把上面的人连起来
# s = "_".join(lst)
# print(s)

# 总结:
'''
1, f"{变量}"     #格式化一个字符串
2, 索引和切片:
    索引: 从0开始的, [0]
    切片: s[start: end: step]   #end位置的数据永远拿不到
3, 相关操作:
    字符串操作对源字符串是不发生改变的
    1. upper()       # 在需要忽略大小写的时候使用
    2. strip()       # 可以去掉字符串左右两端的空白(空格,\t, \n)
    3. replace()     # 字符串替换
    4. split()       # 对字符串进行切割
    5. join()        # 拼接一个列表中的内容成为新字符串
    6. startswith()  # 判断字符串是否以xxx开头
    7. len()         # 字符串长度(内置函数)

    字符串的循环和遍历
    for c in s:
        print(c)      #字符串的每一个字符
    
    关于in:
        1. 判断xxx是否在xxx中出现了
        2. for循环
'''


# import os

# path1 = "/usr/local"
# path2 = "lib"
# path3 = "python3"

# result = os.path.join(path1, path2, path3)
# print(result)  # 输出: "/usr/local/lib/python3"

import os

path1 = os.path.dirname(__file__)
path2 = "lib"
path3 = "python3"

result = os.path.normp          ath(os.path.join(path1, path2, path3))
print(result)  # 输出: "C:\usr\local\lib\python3"(在Windows系统上)

  

posted @ 2023-08-14 11:16  香菜哥哥  阅读(9)  评论(0编辑  收藏  举报