Familiar with Python (2)-Python tutorial

Source: Internet
Author: User
In the previous article, we have discussed a method for connecting two strings. This article continues to describe method 2 for connecting strings, string copying, string length, case-insensitive conversion, and hope to help you. The previous chapter describes how to connect two strings. Review:

>>> a= 'py'>>> b= 'thon'>>> a+b'python'


Since this is a method, there is an additional method.

Method 2 for string connection

Before proceeding to Method 2, we will first describe what is a placeholder. Previously, we mentioned placeholders when explaining variables (parameters). here we will make a strict definition of placeholders:

Definition from Baidu Encyclopedia:

As the name suggests, placeholders are the symbols that occupy a fixed position and wait for you to add content to it.
According to this definition, some placeholders are defined in python. These placeholders are used to describe what type should be entered for that position. here, we will learn about two placeholders: % d -- indicates that the position is an integer, and % s -- indicates that the position should be a string. Let's take a look at a specific example:

The print content must contain a % d placeholder, that is, an integer should be placed at that position. The second % is followed by the things that should be placed in that position. Here is an integer of 1. We will do the following operations to better understand:

>>> A = 1 >>> type ()
 
  
# A is an integer >>> B = "1" >>> type (B)
  
   
# B is a string >>> print "one is % d" % aone is 1 >>>> print "one is % d" % B # An error is returned, the placeholder position should be an integer instead of a string. Traceback (most recent call last): File"
   
    
", Line 1, in
    
     
TypeError: % d format: a number is required, not str
    
   
  
 

Similarly, the position corresponding to % s should be a string. However, if an integer is placed, you can also. However, it is treated as a string. However, we do not agree to do so. In the future, if mysql (a database) is used, % s will be used as the placeholder. this is the case later.

>>> Print "one is % s" % bone is 1 >>>> print "one is % s" % a # The string is inclusive one is 1

Okay. For a long time, do you understand the placeholder? Next we use placeholders to connect strings. Is it interesting?

>>> A = "py" >>> B = "thon" >>> print "% s" % (a, B) # Note

Python
Note: If two placeholders are to be placed in the two positions, the represented items should be written in a parentheses separated by commas (half width.

String replication

There is a variable that connects to a string and also wants to connect another variable to this string. One way is to write the string aside. this method is clumsy and doesn't matter if it is short. But it will be too long. Here is a method for copying strings:

>>> a = "My name is LaoQi. I like python and can teach you to learn it.">>> print aMy name is LaoQi. I like python and can teach you to learn it.>>> b = a>>> print bMy name is LaoQi. I like python and can teach you to learn it.>>> print aMy name is LaoQi. I like python and can teach you to learn it.

Copying is very simple, similar to assigning values. It can be understood that the string was originally connected with a, and B = a, a gave B a rope from its own hand, so that both can point to that string.

String length

To know how many characters a string has, one way is to start from scratch and stare at the screen a few times. Oh, this is not a computer, but a key customer. Key customer, not a swordsman. A swordsman uses a sword as a weapon, while a key hacker uses a keyboard as a weapon. Of course, there are also cheap customers, that is the highest level of the bitch, to the level of the hero, such as the stream of Yue Weiqun.

As a key customer, the string length is as follows:

>>> a="hello">>> len(a)5

The len (object) function is used ). The result is the length of the string.

>>> M = len (a) # assign the returned result to a variable >>> m5 >>> type (m) # the returned value (variable) is an integer.
 

Case-insensitive conversion

For English, case-sensitive conversion is sometimes required. The most famous camper name, which contains uppercase and lowercase parameters. If you are interested, you can refer to the method for automatically converting a string into a camper name.

There are a bunch of built-in functions in python to implement case-insensitive conversion of various types.

S. upper () # uppercase letters in S. lower () # lowercase S in S. capitalize () # uppercase S. istitle () # whether S is the first letter of S. isupper () # whether the letters in S are uppercase S. islower () # whether all letters in S are in lower case


Example:

>>> A = "Hello, World" >>>. upper () # All are uppercase 'Hello, world' >>>. lower () # all lower case 'Hello, world' >>>. capitalize () # upper-case letters, and lower-case 'Hello, wold' >>>. istitle () # whether the first letter is in uppercase. if True is returned, FalseTrue is not returned. >>> a 'Hello, wold' >>> B = "hellow, world" >>> B. istitle () # returns falseFalse if the first letter is not in upper case>. islower () # whether all strings are in lowercase. if true is returned, falseFalse is not returned. >>> B. islower () True

It seems that this article cannot end with the string problem. Continue in the next lecture. Some readers may ask how to use these questions in practice? I want to design a practical scenario for you. can you use what you have learned.

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.