String function

String function

1.capitalize

character string. capitalize () - change the first letter of the string to uppercase and the other letters to lowercase

str = "this is string example from runoob....wow!!!"
str.capitalize()

center() - returns a string centered on the specified width, fillchar is the filled character, and the default is space.

str.center(width[, fillchar])

count() - used to count the number of occurrences of a character in the string. Optional parameters are at the beginning and end of string search

  • sub – substring of the search
  • start – the location where the string starts the search. The default is the first character, and the index value of the first character is 0.
  • end – the position in the string where the search ends. The index of the first character in the character is 0. The default is the last position of the string.
str.count(sub, start= 0,end=len(string))

decode() - decodes the bytes object in the specified encoding format. The default code is' utf-8 '.

bytes.decode(encoding="utf-8", errors="strict")

encode() - encodes the string in the specified encoding format. The errors parameter can specify different error handling schemes.

str.encode(encoding='UTF-8',errors='strict')

6. Endswitch() - used to judge whether the string ends with the specified suffix. If it ends with the specified suffix, it returns True; otherwise, it returns False

str.endswith(suffix[, start[, end]])

expandtabs() - converts the tab symbol in the string to a space

str.expandtabs(tabsize=8)

find() - detect whether the string contains the substring str

str.find(str, beg=0, end=len(string))

isalnum() - detects whether a string is composed of letters and numbers.

str.isalnum()

index() - check whether the string contains the substring str

str.index(str, beg=0, end=len(string))

isalpha() - detects whether a string consists of only letters or text.

str.isalpha()

isdigit() - detects whether a string is composed of only numbers.

isdigit()Method syntax:

13

islower() - detects whether the string is composed of lowercase letters.

str.islower()

isnumeric() - detects whether a string consists of only numbers

str.isnumeric()

isspace() - detects whether the string consists of only white space characters

str.isspace()

istitle() - check whether the spelling initial of all words in the string is uppercase and other letters are lowercase.

str.istitle()

isupper() - detects whether all letters in the string are capitalized.

str.isupper()

18

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

str.join(sequence)

len() - returns the length of an object (character, list, tuple, etc.) or the number of items

str = "runoob"
 len(str)

20

ljust() - returns a new string that is left aligned with the original string and filled with spaces to the specified length

str.ljust(width[, fillchar])

lower() - converts all uppercase characters in the string to lowercase.

str.lower()

22

lstrip() - used to cut off the space or specified character on the left of the string

str.lstrip([chars])

23

maketrans() - conversion table used to create character mapping

str.maketrans(intab, outtab)

24

max() - returns the largest letter in a string.

max(str)

25

min() - returns the smallest letter in a string.

min(str)

26

replace() - replace old (old string) in the string with new (new string)

str.replace(old, new[, max])

27

rfind() - returns the last occurrence of a string

str.rfind(str, beg=0 end=len(string))

28

rindex() - returns the last occurrence of the substring str in the string

str.rindex(str, beg=0 end=len(string))

29

rjust() - returns a right justified original string

str.rjust(width[, fillchar])

30

rstrip() - deletes the specified character at the end of the string

str.rstrip([chars])

31

split() - slice the string by specifying the delimiter

str.split(str="", num=string.count(str))

32

splitlines() - separated by lines ('\ r', '\ r\n', \ n '),

str.splitlines([keepends])

33

Startswitch() - used to check whether the string starts with the specified substring

str.startswith(substr, beg=0,end=len(string));

34

strip() - used to remove the character (default is space) or character sequence specified at the beginning and end of the string.

str.strip([chars]);

35

swapcase() - used to convert the upper and lower case letters of a string

str.swapcase();

36

title() - returns the "captioned" string, that is, the first letter of all words is capitalized

str.title();

37

translate() - convert the characters of the string according to the table (including 256 characters) given by the parameter table

str.translate(table)

38

upper() - converts lowercase letters in a string to uppercase letters.

str.upper()

39

zfill() - returns a string of the specified length, aligned to the right of the original string and filled with 0 in front.

str.zfill(width)

40

isdecimal() - checks whether the string contains only decimal characters.

str.isdecimal()
37

translate() 	-According to parameters table Table given(Contains 256 characters)Convert character of string

```python
str.translate(table)

38

upper() - converts lowercase letters in a string to uppercase letters.

str.upper()

39

zfill() - returns a string of the specified length, aligned to the right of the original string and filled with 0 in front.

str.zfill(width)

40

isdecimal() - checks whether the string contains only decimal characters.

str.isdecimal()

Keywords: Python

Added by Cronje on Sun, 27 Feb 2022 14:19:24 +0200