A method of connecting two strings is already mentioned in the previous chapter. Review:
>>> 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:
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:
>>> 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 are 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.
>>> 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?
>>> 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:
>>> a = "My name is Laoqi." I like Python and can teach to learn it. "
>>> print a My
name is Laoqi. I like Python and can teach your to learn it.
>>> B = A
>>> print b I
name is Laoqi. I like Python and can teach your to learn it.
>>> print a 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:
>>> a= "Hello"
>>> len (a)
5
A function Len (object) is used. The result is the length of the string.
>>> 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 () #S是否是首字母大写的
s.isupper () #S中的字母是否便是大写
s.islower () #S中的字母是否全是小写
See Example:
>>> a = "Hello,world"
>>> a.upper () #都是大写
' hello,world '
>>> a.lower () #都是小写
' hello,world '
>>> a.capitalize () #首字母大写, remaining lowercase
' hello,wold '
>>> A.istitle () #首字母是否是大写, if it returns True, not return false
true
>>> a
' hello,wold '
>> > b= "Hellow,world"
>>> b.istitle () #首字母不是大写, returning false
false
>>> A.islower ( #是否字符串中都是小写, if it returns true, not false
false
>>> b.islower ()
True
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.