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

[formatting method] Python & String

編輯:Python

Common string

# 1.capitalize -> Change the first character to uppercase 
# s = "jack"
# result = s.capitalize()
# print(result)
# 2.casefold -> Compare before string , All changed to the same 
# s = 'Jack'
# s2= 'jack'
# ret = s.casefold() == s2
# print(ret)
# 3.center -> Fill both sides of the string 
# a="jack"
# ret = a.center(50,'-')
# print(ret)
# 4.expandtabs -> It can be seen as a keyboard tab key 
# s = "jack boy"
# ret = s.expandtabs(20)
# print(ret)
# 5.ljust -> Fill left side of string && rjust -> The right side of the string is filled with 
# s = "jack"
# ret = s.rjust(20,'-')
# print(ret)
# 6.lower -> All lowercase && upper -> All variable capitals 
# s = "hello world"
# ret = s.upper()
# print(ret)
# 7.swapcase -> Case interchangeability 
# a = "jACK"
# ret = a.swapcase()
# print(ret)
# 8.title -> Change to title , That is, the first letter of each word is capitalized 
# s = "jack"
# ret = s.title()
# print(ret)
# 9.zfill -> Fill in the empty space of the string 0
# s="Jack"
# ret = s.zfill(20)
# print(ret)
# 10.strip() -> Remove the space on both sides && rstrip() -> Remove the right && lstrip() -> Remove the left side 
# s = " jack "
# ret = s.strip()
# print(ret)
# 11.format() -> Reference external variables 
# msg = "my name is {name}, i am {age} years old. "
# result = msg.format(name="JackMa",age=56)
#
# print(result)

String judgment

# 1.startswith -> Judge initial && endswith -> Judge the tail letter 
# a = "Jack"
# s = a.startswith('J')
# print(s)
# 2.isalnum -> Is it a letter or Numbers isalpha -> Is it a letter 
# s = "jack123"
# ret = s.isalnum()
# print(ret)
# 3.isidentifier -> Is it legal to make the name of the variable 
# s = "1boy"
# ret = s.isidentifier()
# print(ret)
# 4.isnumeric -> Is it a number && Follow isdigit There are some differences 
# age=" twenty-five "
# ret = age.isnumeric()
# print(ret)
# 5.isspace -> Is it a space && isprintable -> Whether it can be printed 
# ret = ' '.isspace()
# print(ret)
# istitle -> Determine whether the initial is capitalized && isupper -> Judge whether the letters are capitalized 
# s = "Hello"
# ret = s.istitle()
# print(ret)

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