自己在自学python中的一些笔记展示,若有错误,恳请指出,我必改正
一、字符串
字符串(string)就是⼀系列字符。在 Python 中,⽤引号引起的都是字符串,其中的引号可以是单引号,也可以是双引号
1.使用方法修改字符串的大小
name="ada luce" print(name.title()) #输出为“Ada Luce”,使用title()方法可以将字符串的每个单词的首字母变成大写再输出 print(name.upper()) #ADA LUCE 大写 print(name.lower()) #ada luce 小写
2.删除前后缀
#使用removeprefix('需要删除的前缀')来删除前缀 jt_url="http://jt.com" print(jt_url.removeprefix('http://')) #jt.com print(jt_url) #http://jt.com jt_url=jt_url.removeprefix('http://') print(jt_url) #jt.com #removesuffix()方法用于从字符串末尾移除指定的后缀。 filename='python_notes.txt' print(filename.removesuffix(".txt")) #输出python_notes #常使⽤ removesuffix() ⽅法来显⽰不包含扩展名的⽂件名,就像⽂件浏览器所做的那样
3.删除字符串左右两端的空白
name=' python ' print(f'"{name}"') #输出" python ",左右两边有空白 #使用rstrip()方法删除右边空白 print(f'"{name.rstrip()}"') #" python" #使用lstrip()方法删除左边空白 print(f'"{name.lstrip()}"')#"python " #使用strip()方法删除两边空白 print(f'"{name.strip()}"')#"python" #将删除空白之后的值赋给原来的变量 name=name.strip() print(f'"{name}"')#"python"
4.在字符串中使⽤变量
#要在字符串中插⼊变量的值,可先在左引号前加上字⺟ f,再将要插⼊的变量放在花括号内。这样,Python 在显⽰字符串时,将把每个变量都替换为其值。 first_name = "ada" last_name = "lovelace" full_name = f"{first_name} {last_name}" print(full_name) #ada lovelace print(f"Hello, {full_name.title()}!") # Hello, Ada Lovelace! #记得加上f,f 字符串是 Python 中用于格式化字符串的一种方式,也被称为格式化字符串字面量(formatted string literals)。 #它允许你在字符串文本中直接嵌入表达式。在字符串前加上字母 f 或 F,然后在大括号 {} 中写入变量名或其他表达式来使用它们的值。
在Python中,大括号 {} 通常用来表示一个占位符或模板变量的位置,特别是在字符串格式化中。当你在字符串中看到 {变量名} 的形式时,这意味着该位置会被对应的变量值替换。
字符串格式化的几种方法:
1. 使用 str.format() 方法 这是较早版本的Python中常用的方法。
例如:
name = "Alice"
age = 25
print("Hello, my name is {} and I am {} years old.".format(name, age))
这会输出: Hello, my name is Alice and I am 25 years old.
2. 使用 f 字符串(格式化字符串字面量) 这是一种更现代且简洁的方法,在Python 3.6及更高版本中可用。
你可以在字符串前面加上 f 或 F,并在大括号 {} 中引用变量:
name = "Alice"
age = 25
print(f"Hello, my name is {name} and I am {age} years old.")
这也同样输出:
Hello, my name is Alice and I am 25 years old.
更复杂的用法 你还可以在大括号 {} 中放置更复杂的表达式,并使用冒号 : 来指定格式化选项,
例如保留几位小数:
price = 19.99
tax_rate = 0.07
total_price = price * (1 + tax_rate)
print(f"The total price with tax is: ${total_price:.2f}")
这会输出: The total price with tax is: $21.39
总结
{变量名} 在字符串中表示一个占位符,它会被相应的变量值替换。 这种格式化字符串的方式适用于多种情况,包括简单的变量插入和复杂的表达式计算。
使用 f 字符串是一种简洁且现代的方法,推荐在Python 3.6及以上版本中使用。
5、练习
#1.个性化消息 # ⽤变量表⽰⼀个⼈的名字,并向其显⽰⼀条消息。 name='Eric' print(f'Hello {name},would you like to learn some python today?') #2.调整名字的⼤⼩写 # ⽤变量表⽰⼀个⼈的名字,再分别以全⼩写、全⼤写和⾸字⺟⼤写的⽅式显⽰这个⼈名 name2="marry" print(name2.lower()) print(name2.upper()) print(name2.title()) #3.找到你钦佩的名⼈说的⼀句名⾔,将这个名⼈的姓名和名⾔打印出来 print('Albert Einstein once said, “A person who never made a mistake never tried anything new.”') #4.重复练习3,但⽤变量 famous_person 表⽰名⼈的姓名,再创建要显⽰的消息并将其赋给变量 message,然后打印这条消息。 famous_person='Albert Einstein' message=f'{famous_person} once said, “A person who never made a mistake never tried anything new.”' print(message) #5.删除⼈名中的空⽩ # ⽤变量表⽰⼀个⼈的名字,并在其开头和末尾都包含⼀些空⽩字符。务必⾄少使⽤字符组合 "\t" 和 "\n" 各⼀次。 # 打印这个⼈名,显⽰其开头和末尾的空⽩。然后,分别使⽤函数lstrip()、rstrip() 和 strip() 对⼈名进⾏处理,并将结果打印出来。 name5='\t\tAlbert Einstein\t\n' print(f'"{name5}"') print(f'"{name5.rstrip()}"') print(f'"{name5.lstrip()}"') print(f'"{name5.strip()}"') #6⽂件扩展名 # Python 提供了 removesuffix() ⽅法,其⼯作原理与 removeprefix() 很像。请将值 'python_notes.txt' 赋给变量 filename # 再使⽤ removesuffix() ⽅法来显⽰不包含扩展名的⽂件名,就像⽂件浏览器所做的那样。 filename='python_notes.txt' print(filename.removesuffix(".txt")) #removesuffix()方法用于从字符串末尾移除指定的后缀。 #请注意,removesuffix()方法只会在字符串的末尾匹配并去除指定的后缀。如果文件名中有多个点(.) # 或者你想要去除的不是标准的文件扩展名,可能需要使用更复杂的方法,比如使用os.path.splitext()函数