Python Learning Notes: Strings

Source: Internet
Author: User

The string type consists of multiple characters that can be used as a whole, or any part of a string.

function Len () returns the length of the string
' www.baidu.com '>>> len (address)13
View CodeTo traverse a string with a for statement

Starting with the first character, reading the characters sequentially, and then doing the corresponding processing until the last character, this process is called traversal.

 for inch address:      ... Print Char
View CodeString fragment

Part of the string is called a fragment, and the operator [n:m] returns part of the string, starting at the nth character to the end of the first m character, including Nth, but excluding the first m. The write-only m means starting at index 0, and write only n for N to the last character.

" Linux Unix FreeBSD " print os[0:5]linuxprint os[6:10]unixprint os[:5]linux  Print os[11:]freebsd
View Code

If we modify one of these characters, the best thing to do is to take part of the string and add or replace the string to form a new string:

" Use python! ">>> new_s = s[0:3]+"  P"+s[5:]print  new_suse python!
View Code

From a string, look for the existence of the given character:

Python Learning Notes: Strings

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.