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

1、 Python learning notes - basic data types - string (2)

編輯:Python

Other string operations

# Other string operations
# Single and double quotation marks
"""
1、 There are single quotes in the string , Use double quotes
2、 There are double quotes in the string , Use single quotes
3、 There are both single and double quotation marks in the string , Use three quotes
"""
str1 = "Let' Go"
print(str1)
str2 = 'My name is "abc"'
print(str2)
str3 = '''My name is "abc". Let' Go'''
print(str3)
# Multiplication sign
"""
1、 How many repetitions
"""
str4 = 'hello'
print(str4*10)
# Take value through index
"""
1、 Specify index location
2、 Specify the index range ( Including the left , Excluding the right )
3、 Specify index range and step ( Stepping , Take every few times )
"""
str5 = 'hello world'
print(str5[1])
print(str5[1:3])
print(str5[1:7:2])
# Members of the operation
"""
1、 Determine whether the content is in the string , The return value is True or False
"""
str6 = 'hello'
print('e' in str6)
# join Method
"""
1、 Combine multiple strings into a new string
2、 Use the specified character or symbol to link the string
"""
a = '123'
b = '456'
c = '789'
print(''.join([a, b, c]))
print(','.join([a, b, c]))
# count Method
"""
1、 Number of statistical elements
"""
str7 = 'hello'
print(str7.count('l'))
# capitalize
"""
1、 Capitalize the first letter
"""
str8 = 'hello'
print(str8.capitalize())
# center
"""
1、 In the middle
2、 Center and specify separator ( Default is space )
"""
str9 = 'hello'
print(str9.center(80))
print(str9.center(80, '-'))
# encode See string encoding for details
# endswith
"""
1、 The return at the end of the judgment is True or False
2、 It can be multiple elements
"""
str11 = 'hello'
print(str11.endswith('o'))
print(str11.endswith('lo'))
print(str11.endswith('a'))
# startswith
"""
1、 The return at the beginning of the judgment is True or False
2、 It can be multiple elements
"""
str12 = 'hello'
print(str12.startswith('h'))
print(str12.startswith('he'))
print(str12.startswith('a'))
# expandtabs
"""
1、 set tabs \t How many spaces are there
"""
str13 = 'hello\tworld'
print(str13.expandtabs(30))
# find
"""
1、 Find the first specified element , And return the index value
"""
str14 = 'hello world'
print(str14.find('o'))
# format
"""
1、 See format output for details
"""
str15 = 'hello {}'
print(str15.format('world'))
# format_map
"""
1、 and format, The passed in value is dictionary
"""
str16 = 'hello {a}'
print(str16.format_map({'a': 'world'}))
# index
"""
1、 and find similar , But if you can't find it, you will report an error
"""
str17 = 'hello'
print(str17.index('h'))
# isalnum
"""
1、 Judge whether it's a number or a letter , The return value is True or False
"""
str18 = 'hello'
print(str18.isalnum())
# isdecimal
"""
1、 Determine if it's decimal , The return value is True or False
"""
print('10'.isdecimal())
print('1F'.isdecimal())
# isdigit
"""
1、 Determine whether a string can be converted to an integer number , The return value is True or False
"""
print('10'.isdigit())
print('abc'.isdigit())
print('1.2'.isdigit())
# isidentifier
"""
1、 Check whether the string can be used as a variable name , The return value is True or False
"""
print('abc'.isidentifier())
print('123'.isidentifier())
# islower
"""
1、 Check whether it is all lowercase , The return value is True or False
"""
print('abc'.islower())
print('aBC'.islower())
# isupper
"""
1、 Check whether it is all uppercase , The return value is True or False
"""
print('ABC'.isupper())
print('aBC'.isupper())
# isspace
"""
1、 Judge if it's all spaces , The return value is True or False.
"""
print(' '.isspace())
print('1 2'.isspace())
# istitle
"""
1、 Is the title format ( Capitalize every word ), The return value is True or False.
"""
print('My Title'.istitle())
print('My title'.istitle())
# toggle case
"""
1、 Capitalize to lowercase
2、 Small to capital
3、 Case interchangeability
"""
print('Hello'.lower())
print('Hello'.upper())
print('Hello'.swapcase())
# Keep left and right
"""
1、 Keep to the left
2、 Keep right
"""
print('hello'.ljust(30, '-'))
print('hello'.rjust(30, '-'))
# strip
"""
1、 Remove the leading and trailing space line break tabs
"""
print(' abc '.strip())
print(' abc\n'.strip())
# lstrip
"""
1、 Remove the leading space line break tab
"""
print(' abc '.lstrip())
# rstrip
"""
1、 Remove trailing space line break tabs
"""
print(' abc '.rstrip())
# replace
"""
1、 Find and replace the specified content
"""
print('abc'.replace('a', 'A'))
# split
"""
1、 Split string , Splits a string by specifying its contents , Output list
"""
print('hello world'.split(' '))
# title
"""
1、 Converts a string to title Format ( Capitalize the first letter of each word )
"""
print('hello world'.title())

String encoding

# String encoding
"""
1、ASCII: Only English numbers and Latin characters can be saved , A character takes up a byte ,8 position
2、gb2312: Only 6700 Multiple Chinese .
3、gbk: 2w Multi Chinese
4、gb18030: 2w7 chinese
5、unicode( unicode , Is a standard, not a code set ):
utf-32 Any character takes up 4 Bytes
uft-16 A character takes up 2 Bytes or more
utf8 One in English ASCII Come and save , Take up a byte , A Chinese account for 3 Bytes
"""
# Encoding and decoding
"""
1、bytes It's a bit stream ( Data flow ), It exists in the form of 01010001110 such
2、str->bytes:encode code
3、bytes->str:decode decode
"""
# demonstration 1、 Converts a string to bytes type , When converting to character type
s1 = ' Coding test '
b = bytes(s1, encoding='utf8')
s2 = b.decode('utf8')
print(s1)
print(b)
print(s2)

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