Python 字符串 - Python零基础入门教程
目錄
- 一.前言
- 二.Python 字符串運算符
- 三.Python 字符串構造
- 四.Python 字符串截取
- 五.Python 字符串替換 – replace()方法
- 六.Python 字符串大小寫
- 七.猜你喜歡
零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門
一.前言
在 Python 變量 文章中我們對 整數 / 浮點數 / bool 值有做過講解,今天詳細在講解一下關于字符串的內容,字符串俗稱:str。
在本文會大量的使用 print 和 format 函數,如果還有不太熟悉使用的盆友,請先跳轉:
- Python print 函數
- Python format 函數
二.Python 字符串運算符
介紹兩個關于 Python 字符串的運算符,in 和 not in,主要用于檢測字符串中是否存在某個字符或者字符串,如果存在返回 True,不存在返回 False,直接上代碼演示:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:python字符串str使用.py @Time:2021/3/21 23:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""# 檢測單個字符 str1 = "hello world" if "h" in str1:print("{} 字符串包含 'h'".format(str1)) # 注意單引號和雙引號的配合使用 else:print("{} 字符串不包含 'h'".format(str1))# 檢測字符串 if "hello" in str1:print("{} 字符串包含 'hello'".format(str1)) # 注意單引號和雙引號的配合使用 else:print("{} 字符串不包含 'hello'".format(str1))# 使用 not in if "hllo" not in str1:print("{} 字符串不包含 'hllo'".format(str1)) # 注意單引號和雙引號的配合使用 else:print("{} 字符串包含 'hllo'".format(str1))''' 輸出結果:hello world 字符串包含 'h' hello world 字符串包含 'hello' hello world 字符串不包含 'hllo''''三.Python 字符串構造
字符串可以直接拼接,同樣也可以使用 format 函數或者 % 符號構造,代碼如下:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:python字符串str使用.py @Time:2021/3/21 23:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""str1 = "hello world" print("%s 字符串總長度:%d" % (str1,len(str1))) # len()獲取字符串長度#方法一: for i in str1:print(i,end="-") # print 函數默認換行,強制將換行符改為 '-',可以改為任意字符print("\n") # "\n" 表示換行 print("*"*20)#方法二: for i in range(0,len(str1)):print(str1[i],end=' ') # 每個字符以空格隔開print("\n") # "\n" 表示換行 print("*"*20)#方法三: a = 0 while a < len(str1):print("str[%d] = %s " % (a,str1[a]))a += 1 print("程序結束,退出程序")''' 輸出結果:hello world 字符串總長度:11 h-e-l-l-o- -w-o-r-l-d-******************** h e l l o w o r l d ******************** str[0] = h str[1] = e str[2] = l str[3] = l str[4] = o str[5] = str[6] = w str[7] = o str[8] = r str[9] = l str[10] = d 程序結束,退出程序'''四.Python 字符串截取
字符串中的每一個字符都有一個默認的索引值,從左到右默認重 0 開始,依次遞增;從右往左默認重-1 開始,依次遞增;
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:python字符串str使用.py @Time:2021/3/21 23:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""str1 = "猿說python" print(len(str1)) # 內置函數 len() 獲取字符串長度 print(str1) # 打印字符串 print(str1[2]) # 獲取字符串中的第二個字符 print(str1[0:2]) # 截取字符串索引值為0~1的字符,不包括索引值為2的字符 print(str1[2:5]) # 截取字符串索引值為2~4的字符,不包括索引值為5的字符 print(str1[2:-1]) # 截取字符串重索引值為2開始直到字符串結尾的前一個,-1的索引值表示最后一個 print(str1[2:len(str1)]) # 截取字符串索引值2~8,最后一個字符的索引值為7,所以剛剛好能截取到字符串末尾# 截取在列表中索引值為0-4的數據,冒號前面不設置參數,默認重0開始,注意截取并不包括4 print(str1[:4]) # 截取在列表中索引值為2-末尾的數據,冒號后面不設置參數,默認截取到最后一位數據,注意截取包括最后一位 print(str1[2:]) print("程序結束,退出程序")''' 輸出結果:8 猿說python p 猿說 pyt pytho python 猿說py python 程序結束,退出程序'''注意:在上面 print(str1[2:-1]) 該行代碼中,-1 表示最后一位字符串索引,但是截取的范圍并不包括字符串的最后一位。
五.Python 字符串替換 – replace()方法
''' 函數介紹:替換字符串中指定的內容,并返回新的字符串old:字符串中需要被替換的字符或者字符串(舊字符串,原本一直就在字符串)new:替換之后的內容(新字符串,添加到字符串代替old的內容) '''str.replace(old, new)示例代碼如下:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:python字符串str使用.py @Time:2021/3/21 23:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""str1 = "hello world" str1 = str1.replace("hello","猿說PYTHON") print(str1)str1 = "hello world" str1 = str1.replace("world","python 教程") print(str1)''' 輸出結果:猿說PYTHON world hello python 教程 '''六.Python 字符串大小寫
對字符串進行大小寫轉換處理,常用的內置函數如下:
-
upper:把所有字符中的小寫字母轉換成大寫字母
-
lower:把所有字符中的大寫字母轉換成小寫字母
-
capitalize:把第一個字母轉化為大寫字母,其余小寫
-
title:把每個單詞的第一個字母轉化為大寫,其余小寫
# !usr/bin/env python # -_- coding:utf-8 \__-""" @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:python 字符串 str 使用.py @Time:2021/3/21 23:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""str = "www.shuopython.com" print(str.upper()) # 把所有字符中的小寫字母轉換成大寫字母 print(str.lower()) # 把所有字符中的大寫字母轉換成小寫字母 print(str.capitalize()) # 把第一個字母轉化為大寫字母,其余小寫 print(str.title()) # 把每個單詞的第一個字母轉化為大寫,其余小寫''' 輸出結果:WWW.SHUOPYTHON.COM www.shuopython.com Www.shuopython.com Www.Shuopython.ComProcess finished with exit code 0'''
關于字符串的函數還有很多,由于篇幅有限,后面的文章我們繼續講解更多關于Python 字符串相關函數。
七.猜你喜歡
未經允許不得轉載:猿說編程 ? Python 字符串
總結
以上是生活随笔為你收集整理的Python 字符串 - Python零基础入门教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BugkuCTF-MISC题善用工具
- 下一篇: BugkuCTF-MISC题神奇宝贝