In operation example Python string variable

. There are a variable name = "aleX leNb" do the following:

name = 'aleX leNb'

1) corresponding to the variable name is removed on both sides of the space values, and outputs the processing result

print (name.strip ()) # spaces on both sides of the string is removed

2) removing the left of the variable name "al" and outputs the processing result

print (name.lstrip ( 'al')) # remove the contents of the string left

3) removing the variable right name "Nb", and outputs the processing result

print (name.rstrip ( 'Nb')) # string right content removed

4) removing the name of the variable at the beginning of a "and the last" b ", and outputs the processing result

s1 = name.lstrip ( 'a') # remove the leftmost 'a'

print (s1.rstrip ( 'b')) # removed rightmost 'b'

Whether 5) determines a variable name to "al" at the beginning, and outputs the result

Whether print (name.startswith ( 'al')) # xxx string is determined at the beginning of

6) determining whether the variable name "Nb" end, and outputs the result

Whether print (name.endswith ( 'Nb')) # xxx string end determination

7) The value of the variable name corresponding to all of the "l" is replaced with "p", and outputs the result

print (name.replace ( 'l', 'p')) # str.replace string replacement (to whom, for whom, several changes)

8) the value of the variable name corresponding to the first "l" replaced by "p", and outputs the result

print (name.replace ( 'l', 'p', 1)) # supra, character replacement

9) The variable name corresponding to a value based on all of the "l" division, and outputs the result.

print (name.split ( 'l')) # string divided

10) The variable name corresponding to the first value based on a "l" division, and outputs the result.

print (name.split ( 'l', 1)) # string divides, several

11) The name of the corresponding variable value becomes capital, and outputs the result

print (name.upper ()) # put all the letters are converted to uppercase

12) The name of the corresponding variable value becomes lower case, and the output

print (name.lower ()) # all the characters in all lowercase

13) the value of the variable name corresponding to the first letter "a" in uppercase, and outputs the result

print (name.capitalize ()) # the first letter of the string into a big

14) determining a value corresponding to the variable name letter "l" appears several times, and outputs the result

print (name.count ( 'l')) # View the number of characters that appear in this string in

15) If it is determined before the value of the variable name corresponding to four "l" appears several times, and outputs the result

print (name.count ( 'l', 0, 4)) # count (to view the characters, start position, end position)

16) to find the "N" corresponding to the index (if it is not found error) from the value corresponding to the variable name, and outputs the result

print (name.index ( 'N')) # View () in the content index is how much, if not directly an error

17) the index to find the corresponding "N" from the value corresponding to the variable name (or -1 if not found) output

print (name.find ( 'N')) # Looking index () in the content, can not find -1

18) to find the "X le" corresponding to the index from the value corresponding to the variable name, and outputs the result

print (name.find ( "X le")) # returns are generally the first character index (TBD do not know right or wrong)

19) Please outputs of the two character values ​​corresponding to the variable name?

print (name [1]) # View second character string, the subscript is zero in

20) Please output the first three characters of the name value of the corresponding variable?

print (name [0: 3]) # output slice [the starting position, the ending position] Gu head regardless of the end

21) Please post the output value of the variable corresponding to the name of two characters?

print (name [-2:]) # - number is a number from the back,: is empty after the last default, default is output from left to right

22) Request output value corresponding to the variable name index where "e" position?

Meaning of the questions is this string in all the indices of all e find out

p = 0
for i in name:
if i == 'e':
print(p)
p += 1**

II. = The string s "123a4b5c
s = '123a4b5c'
. 1) is formed by a new string sections s1 to s, s1 =" 123 ", the establishment of a s1, s cut out to store the contents of
s1 = s [0 :. 3]
Print ( "S1 =" + S1)
2) to form a new string s2 by slicing s, s2 = "a4b", supra, can be arbitrarily selected slice start and end positions
s2 = s [3: 6]
Print ( "S2 =" + S2)
. 3) by forming a new string to s3 s slices, s3 = "1345", the new one, step: reading the first few numbers every
s3 = s [0: . 7: 2]
Print ( "S3 =" + S3)
. 4) is formed by a string of s4 s slices, s4 = "2ab"
supra
s4 = s [. 1:. 7: 2]
Print ( "s4 =" + s4)
. 5 ) is formed by a string of s5 s slices, s5 = "c", - 1 generally refers to the last digit of the string
s5 = s [-1]
Print ( "s5 =" s5 +)
. 6) are formed by sections of the s string s6, s6 = "ba2", here refers -2: from the right to left,
S6 = S [-3 :: - 2]
Print ( "S6 =" + S6)

III. Use the while loop and for each print string s = "asdfer" for each element.
= S "asdfer"
I = 0
a way to print while the string S
while I <len (S):
Print (S [I])
I =. 1 +
a manner for print string S
for I S in:
Print (i)

V. use a for loop to circulate s = "abcdefg", the contents of each print is that each character plus sb, for example: asb, bsb, csb, ... gsb.
Are coupled with each cycle SB
S = 'ABCDEFG'
for I in S:
Print (I + 'SB')

VI used for loop s = "321" to circulate the contents of the print order is: "Countdown 3 seconds", "2-second countdown", "1 second countdown", "go!
It is another format output,
S = '321'
for I in S:
Print ( "second countdown {}" .format (I))
Print ( "Go!")

Seven achieve a calculator integer addition (two numbers):
as: content = input ( "Please enter Content:") a user input: 9 or 5 + 5 + 5 + 9 or 9, and then divided again calculated .
The way to calculate the index adder
content = input ( "Enter a number:")
index = content.find ( '+')
K1 = int (Content [0: index])
K2 = int (Content [index +. 1:] )
SUM = K1 + K2
Print (SUM)

Eight upgrade question: integer addition implement a calculator (multiple numbers together):
as: content = input ( "Please enter Content:") a user input: 5 + 9 + 13 +12+ 6, and then further divided calculation of
7-8 questions are the same, write simple point of it, temporarily keep a small problem, that is, the input letters will complain
content = input ( "enter a number:")
b = 0
for i incontent.split ( "+"):
   + int = B (I)
Print (B)

IX. SUMMARY calculates the user's input has several integer (in units of digits).

Such as: content = input ( "Please enter the text:") # as fhdal234slfh98765fjdla "" "
Content = the INPUT (" look at what you have entered several integer: ")
k = 0
for i in Content:
IF i.isdigit ( ): returns true if # + 1'd
K +. 1 =
Print (K)

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160358.htm