Playing with the old Ziko python string (1)

Source: Internet
Author: User
Tags ming stdin
If the natural language classification, there are many methods, such as English, French, Chinese, and so on, this method is the most common. In linguistics, there are also the methods of classifying languages, such as what language and so on. Here I propose a sub-method, which has not been widely accepted by the masses and researchers, but I believe that the "truth is in the hands of a few", at least here can be used to give themselves courage.

My division: One is two elements of the language (such as two words) and together, out a new element (such as a new word), the other is two elements and together, knowledge two elements side by side. For example "good" and "person", two elements and together is "good person", while 3 and 5 and together is 8, if you think is 35, it belongs to the second class and law.

To abstract this division of my approach:

One is: +-=0
The other is: +-=-
Our language is inseparable from the above two categories, not the first class is the second class.

It's genius. Please clap.

String

In my smug time, I Google a bit, only to find that they are not so smart, read the Wikipedia string entry is said:

A string, which is a finite serial of 0 or more characters. Generally recorded as S=a[1]a[2]...a[n].
See the great Wikipedia, it has taken a situation I have conceived an image of the name, called the string

According to this definition, in the previous two times a programmer felt great "Hello,world", which was a string. or whether in English or Chinese or some other kind of question, the written text can be treated as a string, of course, the inside of the special symbols can also be used as strings, such as spaces.

Practice the string.

>>> print "Good Good study good good study, day up>>> print"----Good---study---day----u P "----Good---study---day----up

After print, a string is printed. Note that the quotation marks are not part of the string, but are enclosed in double quotes. It is telling the computer that it is wrapped in a string. That is, in Python, a string is usually wrapped in a pair of double quotes, or single quotes. Or, to define a string, use double quotation marks or single quotes.

The crossing of thought must have found that there was something wrong with the above remark. What should I do if I want to think of this sentence as a string?

Xiao Ming said, "I didn't burn Yuanmingyuan."

or this sentence

What ' s your name?

The problem is very good, it makes sense. In Python, there is a way to solve a similar problem specifically. Look at the following example:

>>> print "Xiao Ming said:" I did not burn Yuanmingyuan, "Xiao Ming said," I did not burn Yuanmingyuan "

In this example, in order to print the string with double quotes, that is, the double quotation marks are part of the string, a symbol is used: \, in Python, the symbol is called an escape character. A double quotation mark means that the string is included, it is not part of the string, but if preceded by an escape character, it loses its original meaning and translates into a part of the string, which is equivalent to a special character.

The second sentence is printed with the escape character below:

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

In addition, double and single quotation marks can also be nested, such as the following sentence, the single quotation mark inside the double quotation marks, although not in the single quotation mark before the escape character, but it is considered to be part of the string, rather than the symbol of the package string

>>> print "What ' s your name?"  #双引号包裹单引号, the single quote is the character what's your name?>>> print ' What ' Is your "name ' #单引号包裹双引号, double quotes is the character what" is your "name

A variable is connected to a string

The variables are mentioned earlier, and there is a metaphor for fishing. If you forget, see the previous chapter.

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

>>> a=5>>> a5>>> print a5>>> b= "Hello,world" >>> B ' Hello,world ' >> > Print Bhello,world

Remember when we used a type command? Now it is also useful to test a variable, exactly what type is associated with, is it a string or a number?

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

Programmers often use a simple way of saying that A is a digital variable, meaning that it can or has been linked to a number, and B is called a character (string) variable, meaning that it can or has been linked to a string.

Simple manipulation of strings

For numbers, there are some simple operations, such as arithmetic, which, if 3+5, is calculated as 8. So what can you do with strings? Try it:

>>> "py" + "thon" ' Python '

Is it the same as my discovery that is not recognized by most people? The addition of two strings is equivalent to connecting two strings together. (Other operations do not try, no meaning, definitely error, do not believe to try)

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

This is the first action on a string.

Connection string

Method 1:
In the idle, follow the procedure below

>>> a = "Old Qi" >>> b= "teach Python" >>> c = a+b>>> print C old Zishing python>>> C ' \xe8\x80\x8 1\xe9\xbd\x90\xe6\x95\x99python '

This is the simplest way to connect two strings. Notice the last line of the above example, how is garbled? It's not garbled, it's a character encoding problem. This is your right to not see well. But I did see it. Please crossing Google character encoding to know. A very good article is recommended here: Character set and character encoding

Old Qi tip: crossing as a learner, be sure to have a curiosity about the subject, such as the above example, if you are satisfied with the print C, found that the results are as you expect, this is far from enough. If you go down a line, you'll find a weird result, which will make you a big step forward on the programming path. Therefore, to have a curious heart of the world, and constantly explore, think and try. It doesn't cost much to try it on the computer anyway. The worst result is to turn off idle.
Simple manipulation of strings here, the next article continues, not yet, please come again tomorrow.

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