With old Ziko Python's play-round string (2) Update article _python

Source: Internet
Author: User
Tags in python

A method of connecting two strings is already mentioned in the previous chapter. Review:

Copy Code code as follows:

>>> a= ' py '
>>> b= ' Thon '
>>> a+b
' Python '

Since this is a method, by implication, there is another way.

Method of connection String 2

Before saying Method 2, explain what is a placeholder, before explaining the variable (parameter), mentioned the placeholder, here to make a more rigorous definition of placeholders:

The definition from Baidu Encyclopedia:

As the name suggests, the placeholder is the first to occupy a fixed position, waiting for you to add content to the symbol.
According to this definition, there are placeholders in Python that indicate what type of thing that position should be filled in, and here are two placeholders:%d--that the position is an integer,%s--that the position should be a string. Let's look at a concrete example:

Copy Code code as follows:

>>> print ' One is%d '%1
One is 1

There is a%d placeholder in the content required to print (print), which means that the position should have an integer. After the second%, followed by what that position should put. Here is an integer 1. Let's do the following to get a clearer picture of:

Copy Code code as follows:

>>> a=1
>>> type (a)
<type ' int ' > #a是整数
>>> b= "1"
>>> type (b)
<type ' str ' > #b是字符串
>>> print ' One is%d '%a
One is 1
>>> print "One is%d"%b #报错了, the position of this placeholder should be an integer and should not be placed in a string.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError:%d format:a number is required, not str

In the same way,%s should place a string in its corresponding position, but if you put an integer, you can. But it has been converted to a string of treats. But I don't approve of that. In the future, if you use MySQL (a database), you will be required to use%s as a placeholder, this is something, listen to this.

Copy Code code as follows:

>>> print ' One is%s '%b
One is 1
>>> print ' One is%s '%a #字符串是包容的
One is 1

All right. Wordy Half-day, the placeholder is not understood? Below we use placeholders to connect strings. Isn't it fun?

Copy Code code as follows:

>>> a = "py"
>>> B = "Thon"
>>> print "%s%s"% (a,b) #注
Python

Note: Careful observation, if two placeholders, to put things to these two positions, the representative of things to write in a circle of parentheses, and the middle with a comma (half-width) separated.

String copying

There is a variable that connects a string and also wants to have another variable that joins the string. One way to do this is to write a string again, which is a bit clumsy, for short to irrelevant. But the long is in trouble. Here is a method of string copying:

Copy Code code as follows:

>>> a = "My name is Laoqi." I like Python and can teach to learn it. "
>>> Print a
The My name is Laoqi. I like Python and can teach your to learn it.
>>> B = A
>>> Print B
The My name is Laoqi. I like Python and can teach your to learn it.
>>> Print a
The My name is Laoqi. I like Python and can teach your to learn it.

Replication is very simple, similar to assigning values. It can be understood that the string would have been followed by a, and by B=a,a, a string of strings from his hand had been given to B, so that both points could point to that character.

String length

To know how many characters a string has, one way is to start from scratch and stare at the screen to count. Oh, it's not a computer work, it's a key guest at work. A key guest, not a swordsman. Swordsman is a knight with sword as weapon, and key guest is a knight with keyboard as weapon. Of course, there are also a base, that is the highest level of the Yeu Bu, cheap to the degree of the Warrior, such as the flow of the flow.

The key guest so to the number string length:

Copy Code code as follows:

>>> a= "Hello"
>>> Len (a)
5

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

Copy Code code as follows:

>>> m = Len (a) #把结果返回后赋值给一个变量
>>> m
5
>>> type (m) #这个返回值 (variable) is an integral type
<type ' int ' >

Conversion of character capitalization

For English, it is sometimes used for case conversion. Named after the most famous hump, there are some uppercase and lowercase. If you're interested, you can come here to see ways to automatically convert strings into camel-named forms.

In Python, there is a bunch of built-in functions to implement various types of capitalization conversions

S.upper () #S中的字母大写
S.lower () #S中的字母小写
S.capitalize () #首字母大写
S.istitle () #是否单词首字母大写的, and others for lowercase, note that the white feather pointed out that this expression is inaccurate. Thank him very much. In order for reader to have a deeper understanding of these case problems, I'll write down the following example, please reader review. Thanks again for the White Feather.
S.isupper () #S中的字母是否全是大写
S.islower () #S中的字母是否全是小写
See Example:

Copy Code code as follows:

>>> a = "Qiwsir,python"
>>> A.upper () #将小写字母完全变成大写字母
' Qiwsir,python '
>>> a #原数据对象并没有改变
' Qiwsir,python '
>>> B = A.upper ()
>>> b
' Qiwsir,python '
>>> C = b.lower () #将所有的小写字母编程大写字母
>>> C
' Qiwsir,python '

>>> A
' Qiwsir,python '
>>> a.capitalize () #把字符串的第一个字母变成大写
' Qiwsir,python '
>>> a #原数据对象没有改变
' Qiwsir,python '
>>> B = a.capitalize () #新建立了一个
>>> b
' Qiwsir,python '

>>> a = "Qiwsir,github" #这里的问题就是网友白羽毛指出的, thank him very much.
>>> A.istitle ()
False
>>> a = "Qiwsir" #当全是大写的时候, returns false
>>> A.istitle ()
False
>>> a = "Qiwsir"
>>> A.istitle ()
False
>>> a = "Qiwsir,github" #如果这样, also returns false
>>> A.istitle ()
False
>>> a = "Qiwsir" #这样是True
>>> A.istitle ()
True
>>> a = ' Qiwsir,github ' #这样也是True
>>> A.istitle ()
True

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

Follow the White Feather Netizen pointed out, again explore, can do so:

Copy Code code as follows:

>>> A = "This is a book"
>>> A.istitle ()
False
>>> B = a.title () #这样就把所有单词的第一个字母转化为大写
>>> b
' This is A book '
>>> A.istitle () #判断每个单词的第一个字母是否为大写
False

String problem, it seems that this talk is not over yet. The next lecture continues. Have reader may ask, above these in actual combat how to use? I was thinking for you, please key guest design a kind of actual situation, can use to learn.

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.