1. Change the first letter of the string to uppercase
' Welcome to Bokeyuan ' = s.capitalize ()print ()
2. Turn string all to case
' Welcome to Bokeyuan ' = = = S.lower () print (S2) print (S3)
3. Center The character, you can add a fill to replace
s = ' Menu ' s4 = S.center (S5) = S.center (+, '-') print (S4) print (S5)
>>>
Menu
--------Menu--------
4. View the length of the string
s = ' I like reading, Thank ' s6 = Len (s) print (S6) >>>
#标点也算一位
5. Specify the separator to split the specified characters
[' W ', ' lcom ', ' to bOK ', ' yuan '] #结果为列表, and does not contain a delimiter for the specified tessellation
6. Replace
s = ' My name is millet, I love my millet millet ' S11 = s.replace (' millet ', ' rice ') S12 = s.replace (' millet ', ' rice ', 1) S13 = s.replace (' millet ', ' rice ', 2) #s12, S13 followed by the number of different replacements for the number of different print (' S11: ', S11) print (' S12: ', S12) print (' S13: ', S13) >>>S11: My name is rice, I love my rice rice s12: My name is rice, I love my millet millet S13: My name is rice, I love my rice millet
Use of 7.format
Name = input (' Please enter your name: ' ) age= input (' Please enter your age: ') hobby= input (' Please enter your hobby: ') introduce = ' My name is {}, my age is {}, my hobby is {} '. Format (name,age,hobby) print (introduce) >>> Please enter your name: Lucy Please enter your age: 18 Please enter your hobby: Dancing My name is Lucy, my age is 18, my hobby is dancing
Python essay 02