Familiar with Python (2), familiar with python

Source: Internet
Author: User
Tags python list

Familiar with Python (2), familiar with python

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) <type 'int' >#a is an integer >>> B = "1" >>> type (B) <type 'str'> # B is a string> print "one is % d" % aone is 1 >>> print "one is % d" % B # An error is reported, the placeholder position should be an integer instead of a string. Traceback (most recent call last): File "<stdin>", line 1, in <module> 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) # Return the value (variable) is an integer <type 'int'>

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.


2-7 python exercise: the loop and string receive a string input from the user

While

Def main ():
A = raw_input ("Please input a string: \ n ")
I = 0
While I <len ():
Print a [I]
I + = 1

If _ name _ = "_ main _": main ()

For
Def main ():
A = raw_input ("Please input a string: \ n ")
For I in:
Print I

If _ name _ = "_ main _": main ()

Conversion from python list to string

','. Join (list1)

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.