Familiar with Python and character strings (2) Update, learn python

Source: Internet
Author: User
Tags python list

Familiar with Python and character strings (2) Update, learn python

The previous chapter describes how to connect two strings. Review:

Copy codeThe Code is as follows:
>>> 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:

Copy codeThe Code is as follows:
>>> Print "one is % d" % 1
One is 1

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:

Copy codeThe Code is as follows:
>>> A = 1
>>> Type ()
<Type 'int'> # a is an integer.
>>> B = "1"
>>> Type (B)
<Type 'str'> # B is a string
>>> Print "one is % d" %
One is 1
>>> Print "one is % d" % B # An error is reported. The placeholder position should be an integer and should not be 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.

Copy codeThe Code is as follows:
>>> Print "one is % s" % B
One 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?

Copy codeThe Code is as follows:
>>> 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:

Copy codeThe Code is as follows:
>>> A = "My name is LaoQi. I like python and can teach you to learn it ."
>>> Print
My name is LaoQi. I like python and can teach you to learn it.
>>> B =
>>> Print B
My name is LaoQi. I like python and can teach you to learn it.
>>> Print
My 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:

Copy codeThe Code is as follows:
>>> A = "hello"
>>> Len ()
5

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

Copy codeThe Code is as follows:
>>> M = len (a) # assign a value to a variable after the result is returned.
>>> M
5
>>> Type (m) # The returned 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
S. lower () # lowercase letters in S
S. capitalize () # uppercase letters
S. istitle () # indicates whether the first letter of a word is in upper case and the other words are in lower case. Note that the expression here is inaccurate. Thank you very much. In order to give the viewer a better understanding of these case-sensitivity issues, I will write a new example. Thanks again for the white feathers.
S. isupper () # Are all uppercase letters in S?
S. islower () # Are all lowercase letters in S?
Example:

Copy codeThe Code is as follows:
>>> A = "qiwsir, python"
>>> A. upper () # converts lowercase letters into uppercase letters.
'Qiwsir, python'
>>> A # the original data object has not changed
'Qiwsir, python'
>>> B = a. upper ()
>>> B
'Qiwsir, python'
>>> C = B. lower () # Write all lowercase letters into uppercase letters.
>>> C
'Qiwsir, python'

>>>
'Qiwsir, python'
>>> A. capitalize () # converts the first letter of a string into uppercase letters.
'Qiwsir, python'
>>> A # the original data object has not changed
'Qiwsir, python'
>>> B = a. capitalize () # a new
>>> B
'Qiwsir, python'

>>> A = "qiwsir, github" # The problem here is pointed out by white feathers. Thank you very much.
>>> A. istitle ()
False
>>> A = "QIWSIR" # False is returned when all values are uppercase.
>>> A. istitle ()
False
>>> A = "qIWSIR"
>>> A. istitle ()
False
>>> A = "Qiwsir, github" # If so, False is returned.
>>> A. istitle ()
False
>>> A = "Qiwsir" # True
>>> A. istitle ()
True
>>> A = 'qiwsir, Github '# this is True.
>>> A. istitle ()
True

>>> A = "Qiwsir"
>>> A. isupper ()
False
>>> A. upper (). isupper ()
True
>>> A. islower ()
False
>>> A. lower (). islower ()
True

As the White Feather netizen pointed out, you can do this again:

Copy codeThe Code is as follows:
>>> A = "This is a Book"
>>> A. istitle ()
False
>>> B = a. title () # converts the first letter of all words into uppercase letters.
>>> B
'This Is A book'
>>> A. istitle () # determines whether the first letter of each word is capitalized.
False

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.