Familiar with Python (3) and familiar with python

Source: Internet
Author: User

Familiar with Python (3) and familiar with python

String is a topic center.

ID of a string

In many cases, we need to perform operations on each character in the string (see the following content for details). To perform operations accurately, we must perform the task of numbering the characters. For example, if there are 50 students in a class and all of these students have student IDs, it is much easier for teachers to operate on them. For example, you don't need to look for the name of each person. You can use the student ID to know who has handed in the assignment.

In python, the string is numbered in this order: starting from the first on the left, it is numbered 0, and increasing sequentially according to the integer to 1, 2 ..., until the last one, in this process, all characters, including spaces, are improved. For example:

Hello, wor ld

For this string, the order from left to right is:

0 1 2 3 4 5 6 7 8 9 10 11H e l l o , w o r l d

When the class is finished, the teacher only needs to shout out the student's student ID, and the corresponding student automatically stands up. In python, how do I call out the characters corresponding to a number? Check the Code:

>>> A = "Hello, wor ld" >>> len (a) # the string length is 12, which indicates that the string contains 12 public characters, the last character is 1112 >>> a [0] 'H' >>>> a [3] 'l' >>> a [9] ''>>> [11] 'D'> a [5] ','

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

Can I start numbering on the right? Yes. Isn't this small demand for python that people love?

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

Have you seen it? If the first number is-1 from the right, it is separated from the left. That is, a [-1] and a [11] point to the same character.

A character can be accurately located from the left or right. You can choose from which side you like to start or where you want to start based on your actual usage.

String Truncation

With the serial number, you can not only find a character, but also extract part of the string. For example, retrieve "llo" from "hello, wor ld ". You can do this.

>>> a[2:5]'llo'

This is part of the truncated string. Note: The first character (l) in the intercepted part corresponds to the number (2), starting from here; the ending character is (o ), the corresponding number is (4), but the ending number must be increased by 1, not 4, but 5. in this way, what is intercepted is what is required above.

How can I capture ", wor"

That is to say, intercept a [n, m], where n <m, and the obtained characters start from a [n] To M-1].

There are several special

>>> A [:] # indicates intercepting all 'hello, wor ld' >>>> a [3:] # indicates starting from a [3, until the end of the string 'Lo, wor ld' >>> a [: 4] # indicates that 'hangel' is terminated from the beginning of the string until the end of a [4'

Removes spaces at both ends of the string.

This function is useful for users to enter some information. Some friends like to press a space when the input ends, such as asking him to enter his own name. When the input ends, he gives a space. Some users prefer to add a space first. The first character in the input should have two spaces in front of it.

Well, these spaces are useless. Python helps programmers remove these spaces because many people may have this habit.

The method is:

S. strip () removes the Left and Right spaces of the string
S. lstrip () removes the left space of the string
S. rstrip () removes the right space of the string
Before reading the following example, you can use the built-in functions?

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

Exercise

To learn programming, you must do exercises to familiarize yourself with the usage in various situations.

Enter the user name and the computer will automatically greet the 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 needs to input the content. The input content is a string.

In fact, the above Code has this improvement. For example, if the user enters lower-case letters, do you want to change the first letter of the name to upper-case letters? If there are spaces, do you want to remove them? And so on. Maybe there is something else. Can you use the things you have learned before in this exercise?


Use python to drop a column of strings and separate the list with few elements and Output

A column of strings is separated from the list with element 3, which means the string should not contain 3, right?

If so
S = "abc123fg3hi"
S = s. replace ("3 ","")
In this way, you can. The string itself is a type of list, but cannot be written.

Python string processing problem: how to splice the Arc before several strings and the content in it to the end of the string?

#! Python3.2

# Define conversion functions
Def convert (s ):
Index1 = s. index ('(')
Index2 = s. index (')')
Index3 = s. index ('. ')
Return s [index2 + 1: index3] + s [: index2 + 1] + s [index3:]

# Enter your own list ls
Ls = []

# Start Conversion
For s in ls:
Print (convert (s ))

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.