程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python exercise 2 -- string related operations 1

編輯:Python

Catalog

1、 The string “atom” All capitalized

2、 Split string , Make the string “Atom_is_good” Divided into 3 A string “Atom”、“is”、“good”

3、 String concatenation , send ['Atom', 'is', 'good'] Splice into Atom_is_good

4、 Replace string , hold “Atom is good” Replace with “Everybody is good”

5、 Count the number of string occurrences , Statistics "Atom is good" in “o” Number of occurrences

6、 Determine whether the string exists , Judge “m” Whether it exists in the string “Atom” in


1、 The string “atom” All capitalized

Use built-in functions upper()

a = "atom" # Defining variables a For the string atom
print(a.upper()) # Using functions upper() hold atom All capitalized The result is :ATOM

2、 Split string , Make the string “Atom_is_good” Divided into 3 A string “Atom”、“is”、“good”

Use built-in functions split() ( stay () Fill in the symbols that need to be divided )

b = "Atom_is_good" # Define a variable b For the string "Atom_is_good"
print(b.split("_")) # Use built-in functions split() Segmentation 

3、 String concatenation , send ['Atom', 'is', 'good'] Splice into Atom_is_good

Use built-in functions join() Splicing (" Fill in the content to be spliced here ".join( Iteratable objects to splice ))

c= ['Atom', 'is', 'good'] # Define a variable c by ['Atom', 'is', 'good']
print("_".join(c)) # Use .join Function The result is :Atom_is_good

4、 Replace string , hold “Atom is good” Replace with “Everybody is good”

Use built-in functions replace() Replace (replace( Original value , Value to change to ))

d = "Atom is good" # Define a variable d For the string "Atom is good"
print(d.replace("Atom","Everybody")) # Use replace() Function to replace The result is :Everybody is good

5、 Count the number of string occurrences , Statistics "Atom is good" in “o” Number of occurrences

Use built-in functions count() Make statistics   (count( String to count the number ))

e = "Atom is good" # Define a variable e by "Atom is good"
print(e.count("o")) # Use built-in functions count() Make statistics The result is :3

6、 Determine whether the string exists , Judge “m” Whether it exists in the string “Atom” in

There are two ways 1 It's using in Direct judgment

f = "Atom" # Define a variable f For the string “Atom”
print("m" in f) # Use in Judge m Whether it exists in the string f in The result is :Ture

You can also use functions find(), The subscript corresponding to the string will be given , If not, the return value is -1(find( String to find ))

f = "Atom" # Define a variable f For the string "Atom"
print(f.find("m")) # Use built-in functions find() Judge The result is :3

  The official account is two-dimensional code. , The content is sent out synchronously , We can focus on learning together

                                                                                               

This is the official account of Zhang Gouzi's brother brother. , Will share some of the usual work experience , You can pay attention to .

                        ​​​​​​​        ​​​​​​​        ​​​​​​​                                                        


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved