程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python string operation

編輯:Python
Author:AXYZdong Automation   Engineering Male A little bit of thinking , Have a little idea , A little bit rational ! Set a small goal , Try to be a habit ! Meet a better self in the most beautiful years ! More wonderful articles go to :  Personal home page
stay  Python  In the program , If we enclose single or multiple characters in single or double quotation marks , Can represent a string .

Processing strings

  • String creation

>>> s1 = 'hello, world!'
>>> s2 = "hello, world!"
>>> s3 = """ 
hello, 
world!
"""
>>> print(s1, s2, s3, end='')
hello, world! hello, world! 
hello, 
world!
>>> s4 = """ my #  Strings that begin with three double or single quotation marks can wrap
name
is
axyzdong
"""
>>> print(s4)
 my
name
is
axyzdong

  • Escape character



  • Original string

You can add  r , Make it the original string .“ Original string ” Ignore all escape characters at all .
>>> print(r'my name is axyzdong \n')
my name is axyzdong \n
>>> print('my name is \n axyzdong ')
my name is 
 axyzdong

  • String subscripts and slices

>>> spam = 'Hello world!'
>>> spam[0]
'H'
>>> spam[-1]
'!'
>>> spam[0:1]
'H'
>>> spam[0:2]
'He'
>>> spam[:5]
'Hello'
>>> spam[6:]
'world!'

notes :spam[0:1]  in  [0:1] Equivalent to the half open and half closed interval in Mathematics  [0,1)
  • in  and  not in

>>> 'Hello' in 'Hello world!'
True
>>> 'hello' in 'Hello world!' # There's a difference in case
False
>>> 'world' in 'Hello world!'
True
>>> 'my' in 'Hello world!'
False

Common string methods

>>> spam1 = 'hello world!'
>>> print(len(spam1)) # Get string length
12
>>> print(spam1.capitalize ()) # Capitalized copy of string letters
Hello world!
>>> print(spam1.title ()) # Capitalized copy of the first letter of each word
Hello World!
>>> print(spam1.upper ()) # Copy of the string after capitalization
HELLO WORLD!
>>> print(spam1.find ('world')) # Find the location of the string
6
>>> print(spam1.startswith ('he')) # Check whether the string starts with a specific string
True
>>> print(spam1.startswith ('He'))
False
>>> print(spam1.endswith ('he')) # Check whether the string ends with a specific string
False
>>> print(spam1.endswith ('!'))
True
>>> print(spam1.center (20,'*')) # Centers the string with the specified width and fills both sides with the specified characters
****hello world!****
>>> print(spam1.rjust (20,' ')) # Places the string to the right with the specified width and fills the left with the specified characters
 hello world! 
>>> spam2 = '123abc'
>>> print(spam2.isdigit ()) # Check whether the string is all composed of numbers
False
>>> print(spam2.isalpha ()) # Check whether the string is all composed of letters
False
>>> print(spam2.isalnum ()) # Check whether the string is composed of numbers and letters
True
>>> spam3 = ' [email protected] ' # Get a copy of the string after the left and right spaces
>>> print(spam3.strip ())
[email protected]
>>> print(spam3)
 [email protected]

  • Format output string

>>> a, b = 1,2
>>> print('%d + %d = %d'% (a,b,a+b))
1 + 2 = 3
>>> print(f'{a} + {b} = {a+b}') #Python3.6 After simple formatting
1 + 2 = 3

  • pyperclip  Module copy and paste string

pyperclip  Module has copy()  and  paste()  function , Text can be sent to the cutting board of the computer , Or receive text from another .
>>> import pyperclip
>>> pyperclip.copy ('Hello world!')
>>> pyperclip.paste ()
'pyperclip

reference
[1]:https://github.com/jackfrued/Python-100-Days[2]:Python Programming fast : Automate tedious work / ( beautiful ) Svegart (A1 Sweigart)  Writing ; Translated by Wang Haipeng . Beijing : People's post and Telecommunications Press ,2016.7
This sharing is here
If my article helps you 、 If you like the content of my article , please  “ give the thumbs-up ” “ Comment on ” “ Collection ”  One button, three links ! hear    give the thumbs-up    It's not that bad luck for people who are , Every day will be full of vitality !^ _ ^ It's not easy to code words , Everyone's support is my driving force to stick to it . Don't forget to like it   Focus on   I oh ! If any of the above is inaccurate , Welcome to leave a message below . Or you have a better idea , Welcome to exchange and study together ~~~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved