Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the knowledge of Python string

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the knowledge of Python string". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the knowledge of Python string".

1. Partial escape character escape character #\\ backslash str1 = "qqq\\ qq" print (str1) # output qqq/qq#\ b backspace key (Backspace) str2 = "qqq\ b" print (str2) # output qq#\ 'single quotation mark\ "double quotation mark str3 =" qq\' qqqqq\ "print (str3) # output qq'qqqqq" #\ nWrap str4 = "qqqq\ nqq" print (str4) # output Qqqq# qq#\ t Tab (Tab) str5 = "a\ taa" print (str5) # output an aa2.slice slice read string s = "hello world sssss sssss sssss" # s [n] specifies an element in the subscript reading sequence print (s [1]) # e # s [n: M] to read from the subscript value n to MMu1 Several elements print (s [0: 4]) # hell# s [n:] read from the subscript value n to the last element print (s [3:]) # lo world# s [: M] read from the subscript value 0 to print (s [: 5]) # hello# s [:] represents the element print (s [:]) # hello world# s [:-1] that will copy a sequence Column element reverses print (s [::-1]) # dlrow olleh3. Call the split () method to split the string ASCII letter # string .split (delimiter ) # output 26 lowercase letters and reverse the output letters = "" for x in range (97123): letters + = str (chr (x)) print (letters) print ("") print (letters [::-1]) # ord () returns the ASCII code corresponding to the character # chr () returns the character corresponding to the ASCII code # outputs 26 uppercase letters and reverses the output A 65 Z 91letters2 = "" for n in range (65) 91): letters2 + = chr (n) + "" print (letters2) print (letters2 [::-1] .split (", 5)) # string .split (delimiter) Number of separations) 4. The case-dependent method str = "My name in Zyj hello world" # capitalize () only capitalizes the first word, the rest are lowercase print (str.capitalize ()) # My name in zyj hello world# lower () converts letters to lowercase print (str.lower ()) # my name in zyj hello world# upper () converts letters to uppercase print (str.upper ()) # MY NAME IN ZYJ HELLO WORLD# title () each word initials uppercase The rest are lowercase print (str.title ()) # My Name In Zyj Hello World# islower () isupper () istitle () to determine whether the string conforms to the format print (str.isupper ()) # False5. Search for the string str1 = "Myaa namess inddaa Zyjcc helloxx worldbb" # 1.count.py search for the number of specific strings that exist print (str1.count ("aa")) # 2. Find string str.find (character or string, start subscript, end subscript) returns the subscript number of the first time the string was found # find () method returns-1str2 = "My name in Zyj hello world My name in Zyj hello world" print (str2.find ("in",)) # finds the substring in Print (str2.find ("in", 9)) # looks for substring in from subscript number 0, starts with subscript number 9. Str.index (character or string, start subscript, end subscript) returns the specified string subscript value print (str2.index ("name")) # index is different from find, index () returns an error if it cannot be found, find () returns-1 value # 4.startswith (character or string) Start subscript, end subscript) determine whether the character at the beginning of the string contains the subcharacter str3 = "My name in Zyj hello world My name in Zyj hello world" print (str3.startswith ("name", 3)) # True# 5.str.endswith (character or string, start subscript, end subscript) determine whether the character at the end of the string contains the subcharacter print (str3.endswith ("world")) # True thank you for reading The above is the content of "what is the knowledge of Python string". After the study of this article, I believe you have a deeper understanding of the knowledge of Python string, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report