[3] Just learned some simple exercises for Python and several exercises for python

Source: Internet
Author: User

[3] Just learned some simple exercises for Python and several exercises for python
Python dating entertainment club: 613176398

 

 

(1) name = "aleX leNb"

 

1) Remove the spaces on both sides of the value corresponding to the name variable and output the processing result.

Name="AleX leNb"
Print(Name, name. strip())

 

2) Remove 'al' on the left of the name variable and output the processing result.

print(name.lstrip('al'))

 

3) Remove 'nb' on the right of the name variable and output the processing result.

Print(Name. rstrip('Nb'))

 

4) Remove a' and 'B' at the beginning of the name variable and output the processing result.

Print(Name. strip('AB'))

 

5) judge whether the name variable starts with "al" and output the result.

Print(Name. startswith('Al'))

 

6) judge whether the name variable ends with "Nb" and output the result.

Print(Name. endswith('Nb'))

 

7) replace all "l" in the value corresponding to the name variable with "p" and output the result.

Print(Name. replace('L','P'))

 

8) Replace the first 'l' in the value corresponding to the name variable with 'P' and output the result.

Print(Name. replace('L','P', 1))

 

9) split the value corresponding to the name variable according to all "l" and output the result.

Print(Name. split('L'))

 

10) split the value corresponding to the name variable according to the first 'l' and output the result.

Print(Name. split('L', 1))

 

11) change the value of the name variable to uppercase and output the result.

Print(Name. upper())

 

12) write down the value corresponding to the name variable and output the result.

Print(Name. lower())

 

13) uppercase the first letter 'A' of the value corresponding to the name variable and output the result.

Print(Name. capitalize())

 

14) Determine how many times the value letter 'L' corresponding to the name variable appears and output the result.

Print(Name. count('L'))

 

15) how to determine how many times the first four 'l' of the value corresponding to the name variable appears and output the result

Print(Name. count('L', 0, 4))

 

16) Find the index corresponding to 'n' from the value corresponding to the name variable (if not found, an error is reported) and output the result.

Print(Name. index('N'))

 

17) Find the index corresponding to 'n' from the value corresponding to the name variable (if not found, return-1) output result

Print(Name. find('N'))

 

18) Find the index corresponding to 'X' from the value corresponding to the name variable and output the result.

Print(Name. find('X le'))

 

19) Please output the 2nd characters of the value corresponding to the name variable?

Print(Name[1])

 

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

Print(Name[:3])

 

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

Print(Name[-2:])

 

22) output the index location of "e" in the value corresponding to the name variable?

Name="AleX leNb"
Loc= []
ForI, jInEnumerate(Name):
IfJ='E':
Loc. append(I)
Print(Loc)

 

Method 2:

Print(Name. find('E'), Name. rfind('E'))

Method 3:

Print(Name. index('E'), Name. rindex('E'))

23) obtain the subsequence and remove the last character. For example, oldboy gets oldbo.


Print(Name[:-1])

 

 

 

(2) A string s1 = 'alexbarrywusir 'is indexed or sliced to obtain the following results:

 

1) s = 'Alex'

2) s = 'exbarrywusir'

3) s = 'aeb'

4) s = 'axrw'

5) s = 'R' (last r)

6) s = 'abxela'

7) s = 'be'

Answer:

S1='Alexbarrywusir'
Print(S1[:4])
Print(S1[2:-1])
Print(S1[0:5:2])
Print(S1[0:-5:3])
Print(S1[-2])
Print(S1[5::-1])
Print(S1[4:0:-2])

 

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.