Play with old Ziko python spin string (3) _python

Source: Internet
Author: User
Tags in python

A string is a topic center.

Number to String

In many cases, we all have to operate on each character in the string (see later), and to do exactly that, one of the tasks that must be done is to number the characters. For example, there are 50 students in a class, if these students have a number, the teacher to operate them will simplify a lot. For example, do not have to look for each person's name, directly through the school 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, followed by an integer increment, 1, 2 ... until the last one, in this process, all characters, including spaces, are done well. For example:

Hello,wor LD

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

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

In the class, the teacher just shout out the student's study number, automatically have corresponding students stand up. In Python, how do you pull up a character that corresponds to a number? Look at the code:

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

Specifically, the number is from the left, the first one is 0.

Can you start numbering from the right? OK. Isn't it satisfying to have such a human-loving python?

>>> 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, so follow the left to differentiate. That is, a[-1] and a[11] point to the same character.

A character can be found exactly whether it starts on the left or from the right. Reader like where to start from, or depending on which side to start from.

String interception

With numbers, not only can you find a character, but you can also take a part out of the string. For example, remove "Llo" from "Hello,wor ld". This can be done

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

This is part of the intercept string, noting that the first character (L) of the intercepted part corresponds to the number (2), start from here; Fu Shi (o), the corresponding number is (4), but the ending number should be increased by 1, not 4, but 5. This is what is required to intercept.

Try, how to intercept ", wor"

In other words, intercept a[n,m], where the n<m, the resulting character is from a[n] start to a[m-1]

There's a few more special ones.

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

Remove the spaces at both ends of the string

This feature is useful when allowing users to enter some information. Some friends like to enter the end of the time to hit the space, such as let him enter his own name, lost, he came to a space. Some like to first add a space, always do the input of the first word should be empty before two lattice.

Well, these blanks are useless. Python considers 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
Reader before looking at the example below, please use the above built-in function first, is it OK?

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

Practice

Learn to program, you must do exercises, through practice familiar with the use of various situations.

Here's a joint exercise: Enter the username and the computer automatically greets the user. The code is as follows:

#coding: utf-8

print "Please write your name:"
name=raw_input ()
print "hello,%s"%name

The meaning of Raw_input () in this code is to have the user enter the content, and the input is a string.

In fact, the above code has this improvement, for example, if the user entered lowercase, is not the first letter of the name into uppercase? If there are spaces, is it necessary to remove it? Wait a minute. Perhaps there is something else to see if you can use it in this exercise to synthesize what you have learned before?

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.