In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces what is the operation of String string in python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
First, create a new python file named py3_string.py, and write the string manipulation code in this file (the following is the code, and the image shows the running effect after the article):
# define a variable message and assign it to the Hello World string message = 'Hello World'# the printed result will output Hello Worldprint (message)' in order to ensure that the code is readable and easy to understand, the variable name should have a representative meaning. For example, if the variable is named m here, it will be very difficult for others to understand and guess when they see this code. Add spaces before and after variable naming and assignment to increase readability and maintain good coding habits! # assign values to variables using single quotation marks''or double quotation marks'', and three quotation marks (later) # note the following when using single quotation marks # message = 'Yale's World'# the printed result will report SyntaxError: invalid syntax error # because the single quotation mark python after the person's name will be regarded as the end of a string, and the subsequent World' cannot be recognized # print (message) # if you must use single quotation marks At this point, you need to escape the single quotation marks in the string, using the backslash message = 'Yale\' s World'print (message) # or directly using the double quotation marks message = "Yale's World" print (message) # using three quotation marks (the three quotes are all single quotes or the beginning and end of the double quotation marks should be consistent) # variables declared for multiline strings are as follows: message = 'the first time, not fully prepared, nervous Heart racing, but still can not control the excited heart, trembling hands, python I am coming!''print (message) # next look at the use of len () function, print string length: message =' Hello World'# print result is 11print (len (message)) # access the value of the corresponding position in the string through the index subscript: # subscript starts at 0 Print the value returned in the first position Hprint (message [0]) # print the value returned in the last position dprint (message [10]) # if we access the location with subscript 11, there will be an out-of-bounds exception # IndexError: string index out of range#print (message [11]) # Hello in the print string, intercept the middle colon with the index position # the index starts from 0 (including 0) and ends with 5 (excluding 5) The so-called left opening and right closing [0Power5) print (message [0:5]) # also starts not to write the start position, but only writes the end position to get the same result: print (message [: 5]) # gets the World in the string, the starting position is 6, and the end position does not write print (message [6:])
# next learn the method operation of the string # convert the string to lowercase, the print result is: hello worldprint (message.lower ()) # convert the string to large The printed result is: HELLO WORLDprint (message.upper ()) # count the number of occurrences of a string print (message.count ('Hello')) # 1 print (message.count (' l')) # 3 times # use find () to find the index position of the string print (message.find ('World')) # return "find non-existent strings will return-1print (message.find (' Kitty')) # return -replace replace () # with World with Universemessage = 'Hello World'message.replace (' World') ('Universe') # the printed result is still Hello World No surprise, no surprise (cover your face manually) # because the replace method returns a new string value Need to reset a new variable assignment print (message) # declare variable new_message receive, print return result Hello Universenew_message = message.replace ('World','Universe') print (new_message) # string concatenation, use the plus sign greeting =' Hello'name = 'Yale'message = greeting + name# to print the result as HelloYaleprint (message) # the result is about merging two strings together, not what we want Change it to the following: message = greeting +','+ name# print the result as Hello,Yaleprint (message) # use format () to format the output string message ='{}, {}'(greeting,name) print (message) # use the dir () function to view the built-in methods of string # you will see a lot of method functions, not to mention the double underscore method functions for now # first look at what we are familiar with: print (dir (name)) # use the help () function to see the specific usage of string. We can use print (help (str)) as a reference document.
The above code running effect image (the code that caused errors or exceptions has been commented out), help (str) only shows part of the screenshot:
So much for sharing what is the operation of String string in python. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.