Playing with the old Ziko python string (3)

Source: Internet
Author: User
A string is a topic center.

Number A string

In many cases, we have to manipulate each character in the string (see what follows), and one of the things that must be done to do it exactly is to number the characters. For example, there are 50 students in a class, and if the students have a number, the teacher will simplify a lot of things. For example, do not look for each person's name, directly through the study number to know who has not handed in homework.

In Python, the string is numbered in this order: the first from the left is number No. 0, and then increments by integers, 1, 2 ... until the last, in this process, all characters, including spaces, are changed. For example:

Hello,wor LD

For this string, the left-to-right change is:

0 1 2 3 4 5 6 7 8 9 11H e l l o, W o r l D

In the class, the teacher just shout out the student's school number, automatically have corresponding students stand up. In Python, how do you bring up the characters that correspond to a number? Look at the code:

>>> a = "Hello,wor ld" >>> Len (a)   #字符串的长度是12, stating public 12 characters, the last character number is 1112>>> a[0] ' H ' > >> a[3] ' l ' >>> a[9] ' >>> a[11] ' d ' >>> a[5] ', '

In particular, the number is from the left, and the first one is 0.

Can you start numbering on the right? OK. Is it not enough for a man to see a python like this?

>>> a[-1] ' d ' >>> a[11] ' d ' >>> a[-12] ' H ' >>> a[-3] '

Did you see it? If you start from the right, the first number is-1, which distinguishes it from the left. i.e. a[-1] and a[11] are pointing to the same character.

A character can be found exactly as it starts on the left or from the right. Crossing likes to start from which side, or depending on the actual usage, which way to start from.

String interception

With a number, you can not only find a character, but also take a portion of it out of the string. For example, remove "Llo" from "Hello,wor ld". You can do this

>>> A[2:5] ' Llo '

This is part of the Intercept string, note: The first character (L) of the intercepted part corresponds to the number (2), starting from here, the end of the word is (o), the corresponding number is (4), but the end of the number to be increased by 1, not 4, but 5. So the interception is required above.

Try, how to intercept ", wor"

In other words, intercept a[n,m], where n<>< p=""><>

There are a few more special

>>> a[:]  #表示截取全部 ' hello,wor ld ' >>> a[3:]  #表示从a [3] start, until the end of the string ' Lo,wor ld ' >>> A[:4]  #表示从字符串开头一直到a [4] before end ' Hell '

Remove spaces at both ends of a string

This feature is useful when you want to let users enter some information. Some friends like to enter the end of the time to tap the space, such as let him enter his name, lost, he came a space. Some prefer to add a space first, the total input of the first word should be empty before two grid.

Well, these spaces are useless. Python takes into account that many people may have this habit, so it helps programmers to remove these spaces.

The method is:

S.strip () Remove the left and right spaces of the string
S.lstrip () Remove the left space of the string
S.rstrip () Remove the right space of the string
Crossing before looking at the following example, please use the built-in function above, can you?

>>> b= "Hello" >>> B ' Hello ' >>> b.strip () ' Hello ' >>> b ' Hello ' >>> b.lstrip ( ) ' Hello ' >>> b.rstrip () ' Hello '

Practice

To learn to program, you must do exercises to familiarize yourself with the use of various situations.

Here is a joint exercise: Enter the user name, the computer automatically greet this user. The code is as follows:

#coding: Utf-8print "Please write your name:" Name=raw_input () print "hello,%s"%name

The meaning of Raw_input () in this code is that the user enters the content, and the input is a string.

In fact, the above code there is the improvement of the place, for example, if the user input is lowercase, is not to capitalize the first letter of the name? If there is a space, do you want to remove it? Wait a minute. Perhaps there is something else that can be done in this exercise to combine what you've learned in the past?

  • 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.