Home Backend Development Python Tutorial Detailed explanation of how to use str string in python3

Detailed explanation of how to use str string in python3

Mar 24, 2017 pm 04:19 PM
python3 str string

This article mainly introduces the tutorial on the use of str (string) in python3. The introduction in the article is very detailed. The operations of various str strings in python3 are included in this article. Friends who need it can refer to it. , let’s take a look below.

This article mainly introduces a summary of the use of str (string) in python3. The introduction in the article is very detailed. Friends who need it can take a look below.

__add__ function (appends a string at the end)

s1 ='Hello'
s2 = s1.__add__(' boy!')
print(s2)

#输出:Hello boy!
Copy after login

__contains__ (determines whether a string is contained, and returns True if it does)

s1 = 'Hello'
result = s1.__contains__('He')
print(result)

#输出:True
Copy after login

__eq__ (determines two characters Whether the strings are the same, return True if they are the same)

s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)

#输出:False
Copy after login

__format__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__getattribute__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__getitem__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__getnewargs__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__ge__ ( Greater than or equal to)

print('b'.__ge__('a'))

#输出:True
Copy after login
Copy after login

__gt__(greater than)

print('b'.__ge__('a'))

#输出:True
Copy after login
Copy after login

__hash__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__iter__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__len__(return string length)

print('abc'.__len__())

#输出:3
Copy after login

__le__ (less than or equal to)

print('b'.__le__('a'))

#输出:False
Copy after login

__lt__ (less than)

print('b'.__lt__('a'))

#输出:False
Copy after login

__mod__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__mul__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__new__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__ne__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__repr__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__rmod__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__rmul__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__sizeof__

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

__str__(return to self)

print('abc'.__str__())

#输出:abc
Copy after login

capitalize (capitalize the first letter)

s = 'tom'
print(s.capitalize())

#输出:Tom
Copy after login

casefold (convert uppercase to lowercase)

s = 'TOM'
print(s.casefold())

#输出:tom
Copy after login

center (specify the length and padding characters, the content is centered, and the padding characters are left blank if they are spaces)

s = 'Tom'
print(s.center(20,'-'))

#输出:--------Tom---------
Copy after login

count (calculate the number of occurrences of a certain string, the second parameter: starting position, the third parameter: ending position)

s = 'aabbbcccccdd'
print(s.count('cc',3,11))

#输出:2
Copy after login

encode (encoding)

s = "中文"
print(s.encode('gbk'))

#输出:b'\xd6\xd0\xce\xc4'
Copy after login

endswith (to determine whether a string ends with a certain character or string, the second parameter: starting position, the third parameter: ending position)

s = 'Projects'
print(s.endswith('ts'))
print(s.endswith('e',0,5))

#输出:True
# True
Copy after login

expandtabs (convert 1 tab key into 7 spaces)

s = 'H\ti'
print(s.expandtabs())

#输出:H i
Copy after login

find (find the index position of a character or string, second parameter: starting position, third parameter: ending position)

s = 'Hello'
print(s.find('o'))
print(s.find('o',0,3)) #找不到返回-1

#输出:4
# -1
Copy after login

format (String formatting/splicing)

name = 'Tom'
age = 18
s = '{0}\'s age is {1}'.format(name,age)
print(s)

#或者

str = '{name}\'s age is {age}'
result = str.format(age=18,name='Tom')
print(result)

#输出:Tom's age is 18
Copy after login

format_map

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

index (find the index position of a character or string, which is different from find. If the character does not exist, an error will be reported)

s = 'Hello'
print(s.index('o'))
print(s.index('e',0,3))

#输出:4
# 1
Copy after login

isalnum(whether it is a letter or number)

s = '!#'
print(s.isalnum())

#输出:False
Copy after login

isalpha(whether it is a letter)

s = '123'
print(s.isalpha())

#输出:False
Copy after login

isdecimal(whether it is a decimal number)

s = '123'
print(s.isdecimal())

#输出:True

#True: Unicode数字,,全角数字(双字节)
#False: 罗马数字,汉字数字
#Error: byte数字(单字节)
Copy after login

isdigit (whether it is a number)

s = '123'
print(s.isdigit())

#输出:True

#True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
#False: 汉字数字
Copy after login

isidentifier (whether it is an identifier/variable name)

s = '1num'
print(s.isidentifier())

#输出:False
#因为变量名不能以数字开头
Copy after login

islower (whether it is all lowercase letters)

s = 'Hello'
print(s.islower())

#输出:False
Copy after login

isnumeric (whether it is a number)

s = '123'
print(s.isnumeric())

#输出:True

#True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
Copy after login

isprintable (whether it is a printable character/can it be output as is)

s = '\n'
print(s.isprintable())

#输出:False
Copy after login

isspace (whether it is a space)

print(' '.isspace())
print('\t'.isspace())

#输出:True
# True
Copy after login

istitle (whether it is a title/the beginning of each word Letters in uppercase)

print('Hello Boy'.istitle())
print('hello boy'.istitle())

#输出:True
# False
Copy after login

isupper (whether all letters are in uppercase)

print('BOY'.isupper())
print('Boy'.isupper())

#输出:True
# False
Copy after login

join (join the elements in the sequence with specified characters to generate a new string)

s = ['H','e','l','l','o']
print(''.join(s))
print('-'.join(s))

#输出:Hello
# H-e-l-l-o
Copy after login

ljust (Specify the length and padding characters, the content is left-justified, and the padding characters are left blank if they are spaces)

s = 'Hello'
print(s.ljust(10,'-'))

#输出:Hello-----
Copy after login

lower (all strings are changed to lowercase)

s = 'TOM'
print(s.lower())

#输出:tom
Copy after login

lstrip (remove the string The characters specified on the left, the default is a space)

s = ' Tom'
print(s.lstrip())

#输出:Tom
Copy after login

maketrans (Create a conversion table for character mapping, used with the translate function)

intab = "abcde"
outtab = "12345"
trantab = str.maketrans(intab, outtab)

str = "Hello abc"
print (str.translate(trantab))

#输出:H5llo 123
Copy after login
Copy after login

partition (Specify the separator to split the string)

s = 'IamTom'
print(s.partition('am'))

#输出:('I', 'am', 'Tom')
Copy after login

replace (Replace old (old string) in the string with new (new string). If the third parameter max is specified, the replacement will not exceed max times. )

s = 'Tom'
print(s.replace('m','o'))

#输出:Too
Copy after login

rfind(Find the position where the specified string appears from the right, if there is no match, return -1)

s = 'one two one'
print(s.rfind('one'))
print(s.rfind('one',0,6)) #指定起始和结束位置

#输出:8
#  0
Copy after login

rindex(Find the position where the specified string appears from the right, if there is no match If there is a match, an error will be reported)

s = 'one two one'
print(s.rindex('one'))
print(s.rindex('one',0,6)) #指定起始和结束位置

#输出:8
#  0
Copy after login

rjust(Specify the length and padding characters, the content will be right-aligned, and the padding characters will be blank if left blank)

s = 'Hello'
print(s.rjust(10,'-'))

#输出:-----Hello
Copy after login

rpartition( 指定分隔符,从右边开始将字符串进行分割)

s = 'IamTom_IamTom'
print(s.rpartition('am'))

#输出:('IamTom_I', 'am', 'Tom')
Copy after login

rsplit(指定分隔符对字符串进行切片,如果指定第二个参数num,则只分隔num次,最后返回一个列表)

s = 'a b c d'
print(s.rsplit())
print(s.rsplit(' ',2)) #从右边开始,按空格分隔两次

#输出:['a', 'b', 'c', 'd']
#  ['a b', 'c', 'd']
Copy after login

rstrip(删除字符串末尾的指定字符,默认为空格)

s = '!!! I am Tom !!!'
print(s.rstrip('!'))

#输出:!!! I am Tom
Copy after login

split(指定分隔符对字符串进行切片,如果指定第二个参数num,则只分隔num次,最后返回一个列表)

s = 'a b c d'
print(s.split())
print(s.split(' ',2)) #从左边开始,按空格分隔两次

#输出:['a', 'b', 'c', 'd']
# ['a', 'b', 'c d']
Copy after login

splitlines(按换行符来分隔字符串,返回一个列表)

s = 'a\nb\nc'
print(s.splitlines()) #默认参数为False
print(s.splitlines(True)) #指定Ture参数,则保留换行符

#输出:['a', 'b', 'c']
#  ['a\n', 'b\n', 'c']
Copy after login

startswith(判断字符串是否以某个字符或字符串开头的,第二个参数:起始位置,第三个参数:结束位置)

s = 'Projects'
print(s.startswith('Pr'))
print(s.startswith('e',4,8))

#输出:True
#  True
Copy after login

strip(删除字符串前后的指定字符,默认为空格)

s = '!!! I am Tom !!!'
print(s.strip('!'))

#输出: I am Tom
Copy after login

swapcase(大小写互换)

s = 'I am Tom'
print(s.swapcase())

#输出:i AM tOM
Copy after login

title(转换成标题,就是每个单词首字母大写)

s = 'i am tom'
print(s.title())

#输出:I Am Tom
Copy after login

translate(根据maketrans方法创建的表,进行字符替换)

intab = "abcde"
outtab = "12345"
trantab = str.maketrans(intab, outtab)

str = "Hello abc"
print (str.translate(trantab))

#输出:H5llo 123
Copy after login
Copy after login

upper(小写转换成大写)

s = 'Hello'
print(s.upper())

#输出:HELLO
Copy after login

zfill(指定字符串的长度。原字符串右对齐,前面填充0)

s = 'Hello'
print(s.zfill(10))

# 输出:00000Hello
Copy after login

The above is the detailed content of Detailed explanation of how to use str string in python3. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

How to intercept a string in Go language How to intercept a string in Go language Mar 13, 2024 am 08:33 AM

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips Mar 28, 2024 pm 03:02 PM

PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips In PHP development, string processing is a very common requirement. Sometimes we need to process the string to remove extra commas and retain the only commas. In this article, I'll introduce an implementation technique and provide concrete code examples. First, let's look at a common requirement: Suppose we have a string containing multiple commas, and we need to remove the extra commas and keep only the unique comma. For example, replace "apple,ba

See all articles