One of the Python data types string (str)

Source: Internet
Author: User
Tags uppercase letter

A string is one of the most commonly used data types in Python, and the essence of a string is a value, just like a number

It is also easy to create a string by assigning a value to the variable

The value of the note is that the string is not variable and cannot be modified

All strings in Python3 are Unicode strings

Chestnuts:

var = ' Hello world! '

String formatting

String formatting uses the format operator of the string, which is%, to implement

The%s,%d,%.nf part of the formatted string is called the conversion specifier, marking the location of the converted value to be inserted

%s: formatted section for string str, if not string can force type to string

%d: The formatted part is an integer

%F: Formatted as a floating-point number, providing the precision required to retain, a period plus the numbers of decimal digits that need to be preserved

The #%s placeholder,%d integer placeholder,%.nf floating point number, retains several decimals name = input ("Please enter your name:") age = Int ("Your Age is:")) money = float (input ("How much pocket money do you have?") ") Print (" Name is%s, age is%d, pocket money is%.2f "%(Name,age,money)) Run Result: D:\Python\Miniconda3\python.exe d:/python/ python3.6.0/besttest/besttest/recover/day1.py Please enter your name: xxxx your age is: how much do you have pocket money to pinch? 666.666 name is XXXX, age is 18, pocket money is 666.67Process finished with exit code 0       

String method

The string also contains a number of built-in methods, because strings inherit many methods from the string module

Find ()

The Find method can be used to find substrings in a longer string

STR1 = ' to does or not to does, this is a question ' #find方法用来查找字符串中的子串 and returns the subscript print(str1.find (' do ') of the substring at the leftmost position (the first occurrence) #如果查找的元 The -1print(str1.find (' xx ')) operation result: D:\Python\Miniconda3\python.exe 3-1Process finished with exit code 0# Find can also receive the start position and end position parameters, return a range of found substrings (Gu Tou regardless of tail) by providing a starting subscript and an end subscript, str1 = ' to do ' or ' does, ' A question ' Print (Str1.find (' Do ') , 5,20)) operation result: D:\Python\Miniconda3\python.exe16Process finished with exit code 0    

Join ()

A very important string method when joining a method that joins a string element in a list that must be a string type

Inverse method of join for split

Can only change the way of inserting code, server502, and whining ....

#join为字符串的方法, connection string list, inverse method for split # If the element in the list is not a string then an error Dir1 = [' C: ', ' Adminstir ', ' desktop ']sep = ' \ \ ' url = sep.join (dir1) Print (URL) Run Result: D:\Python\Miniconda3\python.exe Yc:\adminstor\desktopprocess finished with exit code 0*************** #list中的内容不是字符串list1 = [5,6]SEP = ' + ' Print (Sep.join (list1)) Run Result: D:\Python\Miniconda3\python.exe Traceback ( Most recent call last):  File "d:/python/python3.6.0/xxxxx/xxxxx/recover/xxx.py", line 178, in <module>    Print (Sep.join (list1)) typeerror:sequence Item 0:expected STR instance, int foundprocess finished with exit code 1

Split ()

The split method is the inverse of join, which splits the string by a character and returns to the list.

#split为join的逆方法, split the string into List # ' \ ' Note escape url = ' c:\\adminstor\desktop ' Dir1 = url.split (' \ \ ') print (DIR1) Run Result: D:\Python\ Miniconda3\python.exe [' C: ', ' adminstor ', ' Desktop ']process finished with exit code 0****************************# When the content in Split is not filled in, str1= ' no Zuo no Die ' Print (Str1.split ()) running results as a space split: D:\Python\Miniconda3\python.exe [' No ', ' Zuo ', ' no ' , ' die ']process finished with exit code 0

Strip ()

The Strip method returns a space or other character that removes both sides (without the middle)

A space that is #strip去除字符串两侧 (without the middle) or the specified string #strip () can only be empty or a string, and the default is empty # to remove whitespace from the string str1 = '      username      ' Print (Str1.strip ()) Operating result: D:\Python\Miniconda3\python.exe Usernameprocess finished with exit code 0************************************** * #去除字符串首尾的指定字符str1 = ' hahaha    username    hahaha ' Print (Str1.strip (' hahaha ')) Run Result: D:\Python\Miniconda3\ Python.exe     username    Process finished with exit code 0

Lower ()

Returns the lowercase letter of a string

#lower返回字符串的小写字母 #upper Returns the uppercase letter of the string # lowercase str1 = ' Hello world! ' Print (Str1.lower ()) Run result: D:\Python\Miniconda3\python.exe Hello world! Process finished with exit code 0# Uppercase str1 = ' Hello world! ' Print (Str1.upper ()) Run Result: D:\Python\Miniconda3\python.exe HELLO world! Process finished with exit code 0
When writing case-insensitive code, you can use the lower or upper method: for example, enter the user name and password when ignoring the case, only need to store and fill in the contents of the login when the whole conversion to uppercase or lowercase can be lower, upper method can also be used with the Strip method , comparing the values entered and stored
#upper () and strip () names = [' DENNY ', ' JENNY ', ' liming ']name = input (' Please enter user name: ') Name = (Name.strip ()). Upper () if name in Names:    print (' Username already exists! ') elif name = = ':    print (' username cannot be empty! ' Else:    print (' Please register first! ')

  

The Replace () replace (' Oldstr ', ' newstr ') method returns a string that is replaced by all occurrences of a string, similar to the Find and replace feature in a Word document
str1 = ' No Zuo no Die ' Print (Str1.replace (' No ', ' no ')) operation result: D:\Python\Miniconda3\python.exe not Zuo not dieprocess finished with E XIT Code 0

  

One of the Python data types string (str)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.