Python Learning Series (3) (string)

Source: Internet
Author: User

Python Learning Series (3) (string)

Python Learning Series (1) (basic)

Python Learning Series (II) (basic knowledge)

I haven't updated my blog for a month. I have been a little busy at work recently. I really didn't stick to it. I lost my time and felt necessary to write my blog. It can be seen that my persistence is not good, this series has no purpose. It is purely amateur study, or a result of temporary interest. Through this article, you can learn the basic operations of strings, learn more exercises over time, and learn how to use strings.

I. Basic Concepts

1. Escape question 1:
1 >>> s="Hello 'Jack'……"2 >>> print s3 Hello 'Jack'……4 >>> 

2) \ escape characters:

1 >>> s="Hello \"Jack\"……"2 >>> print s3 Hello "Jack"……4 >>> 

3:

1 >>> s='''Hello "Jack"……'''2 >>> print s3 Hello "Jack"……4 >>> 

2, special characters:

1) Escape Character: \ n carriage return line break; \ t next stop; \ "Double quotation marks; \ 'single quotation marks; \ output slash

2) original string (output ):

1 >>> print 'E:\temp\node\test.py'2 E: emp3 ode est.py4 >>> print r'E:\temp\node\test.py'5 E:\temp\node\test.py6 >>> 

3. String access:

1) index: the index value starts from scratch and is consistent with that of C.
1 >>> s='www.baidu.com'2 >>> print s[0],s[1],s[2]3 w w w4 >>> 

2) slice slicing: Str_name [Start: end: step] ==> start: Start of the access string; end: end; step: step, + 1 by default

1 >>> s='www.baidu.com'2 >>> print s[2:5]3 w.b4 >>> 

I) positive slice: step is positive, from left to right

Do not specify start:

1 >>> print s[:6]2 www.ba3 >>> 

Do not specify end:

1 >>> print s[4:]2 baidu.com3 >>> 

Neither start nor end is specified:

1 >>> print s[:]2 www.baidu.com3 >>> 

Step is not 1:

1 >>> print s [: 2] 2 wwbiucm (Description: equivalent to ww w. ba id u. co m separated by spaces) 3 >>>

Ii) Negative slice: step is a negative number and the direction is from right to left.

Start is positive, end is negative, and step is negative:
1 >>> print s[13:-3:-1]2 mo3 >>> 

Start is negative, end is negative, and step is negative:

1 >>> print s[-1:-3:-1]2 mo3 >>>

Neither start nor end is specified, and step is-1:

1 >>> print s[::-1]2 moc.udiab.www3 >>> 

4. String operations:

1) addition:
1 >>> s1 = 'www .baidu.com '2 >>> s2 = 'python' 3 >>> print s1, s2 # comparison of comma usage 4 www.baidu.com python5 >>> print s1 + s26 www. baidu. compython7 >>>

2) multiplication: equivalent to adding n times of the same character (string) (unique to Python)

1 >>> print s1*22 www.baidu.comwww.baidu.com3 >>>

3) (not) in operation: determines whether a character (string) is in a string. If (NO) exists, the return value is true. Otherwise, the return value is false.

1 >>> print 'w' in s12 True3 >>> print 'k' in s14 False5 >>> print 'k' not in s16 True7 >>> 

5. String functions:

Example:

1 >>> print s1.find('w',2)2 23 >>> print s1.find('j')4 -15 >>> print s1.count('w')6 37 >>> print s1.replace('w','k',2)8 kkw.baidu.com9 >>> 

Ii. Practical drills (this part will be followed by a complete example)

1. Extract the hyperlink address from the webpage

1) analysis steps: (programming is very important !!!)

  • Browse Web http://www.cnblogs.com/zhangbc/
  • Analyze Web code
  • Take the first connection
  • Print
1 s = "<a href =" http://www.cnblogs.com/zhangbc/p/3501205.html "class =" c_ B _p_desc_readmore "> read the full text </a>" 2 url = s [s. find ('href ') + 6: s. find ('html ') + 4] 3 print url

The URL is loaded in the browser:

1 import webbrowser as web # introduce a third-party library and use as to get the alias 2 url = 'HTTP: // www.cnblogs.com/zhangbc'3 web. open_new_tab (url)

Close the browser:

1 import os2 import time3 time. sleep (10) 4 5 OS. system ('taskkill/F/IM sogoupolicer.exe ') # Note Space

Iii. Summary

This article describes how to use Python strings. through practice, you should have a preliminary understanding of Python strings.

 

 

 

 

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.