Python Learning Day3

Source: Internet
Author: User

Index and slice of a string
str_1 ='Neroisgoodman'str_2= Str_1[5]#Index Single character
>>>sStr_3 = str_1[0:]#all slices from start to end
>>>neroisgoodmanStr_4 = Str_1[:4]#0-4 character slices, Gu Tou regardless of tail
>>>neroStr_5 = str_1[4:]#from index 4 to last slice
>>>isgoodmanStr_6 = Str_1[::-1]#Reverse
>>>namdoogsiorenstr_7 = Str_1[-1::-1]#Reverse
>>>namdoogsiorenStr_8 = str_1[-7:]#all slices backward starting from the 7th character to the bottom
>>>goodmanStr_9 = Str_1[1::2]#Slice from the beginning of the first character to the last character
>>>eosoda
Common functions for strings
Nero ='Nero is good man666'Print(Nero.capitalize ())#Capitalize first letter capital>>>nero isGood man666
Print(Nero.swapcase ())#swapcase Uppercase and lowercase letters reversed>>>NERO is good MAN666
Print(Nero.title ())#title is capitalized in letters with spaces, numbers, and special character intervals>>>Nero is good Man666
Print(Nero.center (30,'*'))#Center content centered, if the total length of the string is not large parameter length, the parameter exceeds the length by the specified character substitution, such as the parameter does not have the total length of the string, the substitution character is not displayed>>>*****nero isGood man666******
Print(Nero.count ('o', 4,-1))#The number of occurrences of the same character in the count statistic string, which can be sliced>>>2
Print('Nero\tnero'. Expandtabs ())#Expandtabs By default, a tab key becomes 8 spaces, if the character in front of the tab is less than 8, then complete 8, if the character in front of the TAB key is more than 8 less than 16, then complete 16, and so on each complement of 8. >>>Nero Nero
Print(Nero.startswith ('Nero'))#StartsWith to determine whether a string is ... Begins with a Boolean value that can be sliced>>>True
Print(Nero.startswith ('Nero', 8,-1))>>>False
Print(Nero.endswith ('again'))#EndsWith to determine whether a string is ... End, returns a Boolean value that can be sliced>>>False
Print(Nero.endswith ('Good', 6,12))>>>True
Print(Nero.find ('Good'))#Find search in the string if the character exists, there is returned index found, there is no return-1>>>8
Print(Nero.index ('Good'))#Index looks for the character in the string, exists, returns the found index, no error exists>>>8
Print(Nero.split ())#Split splits the string , slices the string by the specified delimiter, and returns the segmented list of strings, separating defaults from the space, but not empty.>>>['Nero',' is','Good','man666']
Print('{}{}{}'. Format ('Nero',' is','Goodman'))#three usage of format formatted output>>>Neroisgoodman
Print('{0}{1}{0}'. Format ('Nero',' is','Goodman'))>>>Neroisnero
Print('{}{}{}'. Format (Nero,nero,nero))>>>nero isGood Man666nero isGood Man666nero isGood man666
Print(Nero.strip ('*'))#strip deletes the specified character at both ends of the string, removing both spaces by default>>>nero isGood man666
Print(Nero.lstrip ('*'))#Lstrip deletes the specified character to the left of the string and removes the space by default>>>nero isGood man666
Print(Nero.rstrip ('*'))#Rstrip Deletes the character specified to the right of the string and removes the space by default>>>nero isGood man666
Print(Nero.replace ('Nero','Alex', 1))#replace the characters in the string, you can specify the number of replacements>>>alex isGood man666
Print(Nero.isalnum ())#Isalnum Determines whether a string consists of letters or numbers, and a space or special character returns false>>>False
Print(Nero.isalpha ())#Isalpha Determines whether a string consists of only letters, with spaces or special characters returning false>>>False
Print(Nero.isdigit ())#IsDigit Determines whether a string consists of only numbers, and a space or special character returns false>>>false

Python Learning Day3

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.