[Basic learning python] Playing with character strings (1)

Source: Internet
Author: User

[Basic learning python] Playing with character strings (1)
For natural language classification, there are many separation methods, such as English, French, and Chinese, which are the most common. In linguistics, there are also Classification Methods for languages, such as what languages. I propose a method, which has not yet been widely recognized by the masses and researchers. However, I believe that "truth is in the hands of a few people ", at least here, it can be used to make yourself brave.

My division: two elements (such as two words) in a language are combined to create a new element (such as a new word ); the other is that two elements are combined, and the two elements of knowledge are tied together. For example, "good" and "man", two elements are "good" together, and 3 and 5 are 8 together. If you think it is 35, that is the second type and method.

Abstract my method:
One is: △+ □= ○ The other is: △+ □= △□
In our language, we can't do without the above two types, either the first type or the second type.

It's a genius. Applaud.


String
When I was so self-satisfied, I googled a bit and found that I was not so clever. Here is what I said when I read the string entry in Wikipedia:

[String) is a finite serial consisting of zero or multiple characters. It is generally recorded as s = a [1] a [2]... a [n].]

Let's see the greatness of Wikipedia. It has taken an image name for a situation I have imagined, called a string.

According to this definition, the previous two "Hello, World" that made a programmer feel great is a string. In other words, the written text can be treated as a string regardless of English, Chinese, or other types of questions. Of course, the special characters in the string can also be used as strings, such as spaces.

Drill down the string.

>>> print "good good study, day day up"good good study, day day up>>> print "----good---study---day----up"----good---study---day----up

After print, all printed strings are printed. Note that it is in double quotation marks, which are not part of a string. It is telling the computer that it contains a string. In python, a pair of double quotation marks or single quotation marks are usually used to enclose a string. To define a string, use double quotation marks or single quotation marks.

Those who love thinking will surely find that the above sentence has a problem. What should I do if I want to regard the following sentence as a string?

James said, "I have not burned yuan mingyuan"

Or this sentence

What's your name?

The problem is very good and makes sense. In python, there is a way to specifically solve similar problems. See the following example:

>>> Print "James said: \" I Don't Have Shao Yuanmingyuan \ "" James said "I Don't Have Shao Yuanmingyuan"

In this example, In order to print the string containing double quotation marks, that is, double quotation marks are part of the string, a symbol is used: \, in python, this symbol is called an escape character. Double quotation marks indicate that the string is included. It is not a part of the string, but if there is an escape character before it, it loses its original meaning and is converted to a part of the string, which is equivalent to a special character.

The following uses an escape character to print the second sentence:

>>> print 'what\'s your name?'what's your name?

In addition, double quotation marks and single quotation marks can be nested. For example, in the following sentence, single quotation marks are in double quotation marks. Although they are not added before single quotation marks, they are considered as part of strings, instead of the character string.

>>> Print "what's your name? "# Enclose single quotes in double quotes. Single quotes are the characters" what's your name? >>> Print 'what "is your" name' # enclose double quotation marks in single quotes. Double quotation marks indicate the character "is your" name

Variable connection string


As mentioned above, there is a metaphor for fishing. If you forget it, see the previous chapter.

In fact, variables can not only be connected to numbers, but also strings.

>>> a=5>>> a5>>> print a5>>> b="hello,world">>> b'hello,world'>>> print bhello,world
Remember that we used a type command? Now it is still used to test a variable. What type is it related to? Is it a string or a number?

>>> type(a)
 
  >>> type(b)
  
 

Programmers often use a simple saying that a is called a numeric variable, which means that it can or has been connected to a number. B is called a string variable, it means that it can or has been connected to a string.
Simple string operations

There are some simple operations on numbers. For example, the four arithmetic operations are: If 3 + 5, 8 is calculated. What operations can be performed on strings? Try it:

>>> "py"+"thon"'python'

This is the same as my findings that are not recognized by most people. Do you still not agree? Adding two strings is equivalent to connecting the two strings. (Don't try other operations. It makes no sense. You must report an error. If you don't believe it, try it)

>>> "py"-"thon"Traceback (most recent call last):  File "
 
  ", line 1, in 
  
     TypeError: unsupported operand type(s) for -: 'str' and 'str'
  
 

The above is the first operation on the string.


Connection string


Method 1:


Perform the following operations in IDLE:

>>> A = "Old Qi" >>> B = "teach python" >>> c = a + B >>> print c old Qi teaches python >>> c '\ xe8 \ x80 \ x81 \ xe9 \ xbd \ x90 \ xe6 \ x95 \ x99python'

This is the simplest method to connect two strings. Note that the last line in the above example shows garbled characters? It's not garbled, it's about character encoding. You can't see this. But I did see it. Please refer to the official google character encoding. We recommend a very good article: Character Set and character encoding.

TIPS:As a learner, you must be curious about the objects you have learned. For example, if you are satisfied with print c and find that the results are the same as you expected, this is far from enough. If you go down a line, you will find a strange result, which makes another huge step forward in programming. Therefore, you must be curious about the world and constantly explore, think, and try. It does not cost much to try on the computer anyway. The worst result is to turn off the IDLE.

Simple string operations are now available. The next article will continue. Please come back tomorrow.


Statement

This blog cannot publish hyperlinks and URL addresses."A perfect version of zero basic learning python"




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.