Python third day, string continuation

Source: Internet
Author: User
Tags tidy

  

String type

In Python, string types can be used in pairs of single quotes, such as: ' Hello, China. can also be used in pairs of double quotes, such as: "Bad, I drop country." The code is as follows:

  

Through. Title (), you can turn the first letter of a paragraph into uppercase letters, such as: "I love you!"

  

Use. Upper () and. Lower () to make the string all uppercase or lowercase, such as:

  

Method Lower () is useful when storing data. Many times, you can't rely on users to provide the correct case, so you need to say that the strings are converted to lowercase before they are stored. When you need to display this information later, you are converting it to the most appropriate case.
This situation is very useful when we enter the website verification code, we often encounter such problems when registering a new website, the website asks us to enter the verification code, but no matter whether we enter the uppercase or lowercase are not related.
If: The site provides a random verification code, uppercase and lowercase, there must be a conversion mechanism, the verification code we entered into a unified format in the background generated by the verification code comparison, such as:

#Web site randomly generated verification code, with uppercase and lowercaseSite_verification_code ="ABADF"#Enter the user to see the verification code, the user can enter freely, regardless of the case is not relatedUser_verification_code = input ("Please enter the verification code:")ifUser_verification_code.lower () = =site_verification_code.lower ():#If the user enters a verification code (converted to lowercase format) with the site randomly generated verification code is consistent, then the user can enter the site, or you will need to re-enter    Print("Welcome to Baidu.")Else:    Print("The code you entered are incorrect, please re-enter:") Results:please Enter the verification code:abadfwelcome to Baidu

We enter ABADF or ABADF, as long as the site is randomly generated by the verification code consistent, the final will be entered into the site, which is similar to the verification Code authentication mechanism;
If we enter the wrong verification code, such as ACADF, see how the result is as follows:

Is

This time the program run results prompt us, we entered the verification code is wrong, we need to re-enter, of course, this program does not join the Loop statement, this can be added loop, can be infinite loop, can also set the number of inputs, programming is the real world description, in the programming embodiment of it.

Merging stitching strings

In many cases, you need to merge strings. For example, we might store the first and last names in different variables, and then merge them together when we want to display names:

  

Python uses a plus sign (+) to combine strings. In this example, we use + to merge first_name, spaces, and last_name to get the full name, in Python, programming is beautiful, the program looks neat and tidy, so you need to decorate the code at all times to make the code look neat and tidy.
For example, add some spaces, line breaks, special symbols (-, "", etc.), tabs, etc., so that the code looks neat and orderly, so that people have a sense of enjoyment, refreshing.

Use tabs or newline characters to add whitespace

In programming, whitespace refers to any nonprinting character, such as spaces, tabs, and line breaks. We can use whitespace to organize the output so that the code is easier to read and aesthetically pleasing to order.
To add a tab to a string, use the character combination \ t; To add a newline character to a string, you can use a combination of characters \ n:

  

  

Remove Blank

In the program, the extra whitespace can be confusing. Like "Python" and "python" look pretty much the same, but for the program they are two different strings, Python can find extra whitespace in "python" and think they are meaningful-unless you tell it is not.

We'll learn how to get the string length before we learn to take the trailing blanks, and we'll use Len (),

With this method, we can see that there are three spaces in the subject, then we use Rstrip () to ensure that there is no space at the end is to remove the end of the 3 spaces,

  

  

In the above program, we have processed the program, output the original program and length, as well as the length after the exclusion of space, you can see, in Python, plus space and no space is not the same, when necessary to eliminate the extra space.
There are three ways Python proposes spaces, Rstrip (), Lstrip (), and strip ().
The meaning of the word strip is: Remove, peel, remove, delete, clear, dismantle, in Python the main is to remove the space, Rstrip () is to delete the word to the right of the space, Lstrip () is the abbreviation of Left+strip, meaning to delete the left side of the word, make the word strip () is to delete the spaces on either side of the word;
Learn the Excel function should know, Excel function Trim () is also to remove the space, but it can delete the middle of the word space, in Python strip () method is not possible, can only delete the space on both sides:
For example: In Excel, Trim ("I love python") = "I love Python", but in Python, using the Strip () method, the result is: "I love Python" is unable to delete the middle character of the string.
However, this deletion is only temporary and does not really remove the right-hand space from the favorite_language, but we get the results we want to see when we output the results.
To permanently delete whitespace from this string, you must save the result of the delete operation back into the variable:
Favorite_language = Favorite_language.rstrip ()
To remove whitespace from this string, you need to delete the whitespace at the end of it, and save the result back to the original variable. In programming, it is often necessary to modify the value of the variable to save the new value back to the original variable. This is why the value of a variable may change depending on the operation of the program or the user's input data.

Numeric to String

In the development process, many times we will use the string and the number of combinations, if the forced conversion will be error,

  

So if you want to use the two types together, this time we use the str () method,

  

Must be converted to a string when there are numbers to merge stitching

Avoid syntax errors when using strings

Python to enter the word is very strict, case, character format, etc. must be strict Finally, there is a place error system will be error, I also often encounter such problems, such as string and digital splicing, often forget to convert the number into a string, so the system will error.

The Zen of Python

The programming language Perl had dominated the internet for a long time, and most of the early interactive sites used Perl scripts. At that time, "there are multiple solutions to the problem" is the motto of the Perl community.
This idea was once loved by everyone because of the inherent flexibility of the language, which led to many different solutions to most problems. This flexibility was acceptable during the development of the project, but it was finally recognized that
Too much emphasis on flexibility can make it difficult to maintain large projects: it is difficult and cumbersome and time consuming to find out what the people who solved the complex problem were thinking by studying the code.
Experienced programmers advocate as far as possible to avoid the complexity of Jane, the concept of the Python community is included in Tim Peters written by the "Zen of Python." To learn about these guidelines for writing good Python code, you only need to execute commands in the interpreter
Import this. will be able to view the principles of writing procedures, guiding opinions.
  

the Zen of Python, by Tim petersbeautiful isbetter than ugly. Explicit isbetter than implicit. Simple isBetter than Complex.complex isbetter than complicated. Flat isbetter than nested. Sparse isbetter than dense. Readability counts. Special cases aren'T special enough to break the rules.Although practicality beats purity. Errors should neverPasssilently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- andPreferably only one--obvious-do it. Although that, may notBe obvious at first unless're Dutch.Now isbetter than never. Although never isOften better than *right*Now . If the implementation isHard to explain, it's a bad idea.If the implementation isEasy to explain, it is a good idea. Namespaces is one honking great idea-Let's do more than those!

Beautiful is better than ugly.
Python programmers believe that code can be written beautifully and elegantly. Programming is about solving problems, and a well-designed, efficient and beautiful solution will pay tribute to the programmer. As you learn more about Python,
The deeper, and use it to write more and more code, one day maybe someone will stand behind you and exclaim: Wow, the code is very bright! ”

Simple is better than complex.
If there are two solutions, one simple, one complex, but all effective, choose a simple solution. This way, the code you write will be easier to maintain, and you or someone else can improve the code later
More easily.
Complex is better than complicated.
The reality is complex, and sometimes there may not be a simple solution. In this case, choose the simplest possible solution.
Readability counts.
Even complex code makes it easy to understand. When you develop a project that involves complex code, be sure to write useful comments for the code.
There should be one – and preferably only one--obvious it.
If you let two Python programmers solve the same problem, the solution they provide should be roughly the same. This is not to say that programming has no creative space, but rather the opposite! However, most of the programming work is
Use common solutions to solve simple small problems, but these small problems are included in a larger, more creative space of the project. In your program, the specifics of the details should be easy to understand for Python programmers.

Python third day, string continuation

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.