The string is the most commonly used data type in Python. We can use quotation marks (' or ') to create a string.
Creating a string is simple, as long as you assign a value to the variable. For example:
#!/usr/bin/env python
# name= ' Hello '
name1= Python '
Here are some of the characters commonly used in Python to create an operational collation. If not all please forgive!
#!/usr/bin/env python# the basic operation of the string name= ' Hello ' name1= ' python ' #+ plus used to concatenate strings print (name + name1) #运行结果为HelloPython #* multiplication sign, used to repeat the character create print (name*2) # run the result as:hellohello#[] brackets according to the index ( That is, the location and the list are all starting from 0) print (name[0]) #如果是0的话打印出来的就是H, if 1 is e# ":" Intercept word a part: such as "0:3" print (Name[0:3]) #运行结果为:hel#in represents a character that is present in the string when returned trueif ' H ' in name: print (' h in name variable ') #not in If a character does not exist in the string, return trueif ' y ' not in name: print (' y does not exist in Variable name ') #python对于字符串的内建常用操作方法 #center Specifies a string,- with a python-centered width of 50 as the fill symbol. print (' Python '. Center (+, '-')) #运行结果为:------------python------------#count stats ' l ' There were several print (' Hello '. Count (' l ')) in the "Hello" character Genesis #endswith to determine if the string ends with ' lo ' and then returns a true value print (' Hello '). EndsWith (' lo ')) #startswith determines if the string is preceded by ' he ' , and returns a true value print (' Hello '. StartsWith (' He ')) #index   Find h the index in the string is also the location. print (' Python '. Index (' h ')) #运行结果是 h The index position in the Python string is 3#find find y position in the string, Equivalent to index , but find if not in the string returns a value of-1 print (' Python ' .find (' y ')) #strip Eliminate spaces one = input (' Please enter a word prompt: ') if one.strip () == ' Wang ': print (' you entered the correct ') # Run results is the input Wang this string if followed by a space, will also be recognized as correct, if there is no strip this parameter when the input wang with a space will be an error #isdigit () To determine whether you enter a pure number, Is the number that returns truetwo = input (' Please enter Number: ') ' 123 '. IsDigit () if two.isdigit (): print (' You enter a number ') Else: print (' Please enter a pure number ') #.isalnum () Determine if the input contains special characters, No special characters are returned Truestr=input (' Please enter your content: ') if str.isalnum (): print (' input correct ') Else: print (' Please do not enter special characters ')
This article is from the "Carefree" blog, please be sure to keep this source http://birdcai.blog.51cto.com/11216068/1828221
Python Learning-2 days--string manipulation