1. ホーム
  2. スクリプト・コラム
  3. パイソン

Pythonの文字列の詳細

2022-01-26 04:05:43

1. 部分的にエスケープされた文字

Escape characters
# \\\ Backslash
str1 = "qqq\\qq"
print(str1)
# Output qqq/qq
# \b Backspace
str2 = "qqq\b"
print(str2)
# Output qq
# \' single quotes \"double quotes
str3 = "qq\'qqqqq\""
print(str3)
# Output qq'qqqqqqq"
# \n line feed
str4 = "qqqq\nqq"
print(str4)
# output qqqq
# qq
# \t Tabulator (Tab)
str5 = "a\taa"
print(str5)
# Output a aa

2.スライス文字列の読み込み

s = "hello world sssss sssssss sssssss"
# s[n] specifies a subscript to read an element in the sequence
print(s[1])
# e
# s[n: m] reads from subscript n to m-1, a number of elements
print(s[0: 4])
# hell
# s[n:] reads from subscript n to the last element
print(s[3:])
# lo world
# s[:m] reads from subscript 0 to m-1 elements
print(s[:5])
# hello
# s[:] means that a copy of the sequence is made
print(s[:])
# hello world
# s[::-1] reverses the entire sequence of elements
print(s[::-1])
# dlrow olleh

3. ASCII文字列を分割するためにsplit()メソッドを呼び出します。

# string.split(separator, number of separations)
# Output 26 lowercase letters and invert the output
letters = ""
for x in range(97, 123):
    letters += str(chr(x))
print(letters)
print(" ")
print(letters[::-1])
# ord() returns the ASCII code corresponding to the character
# chr() returns the character corresponding to the ASCII code
# output 26 uppercase letters and invert the output A 65 Z 91
letters2 = ""
for n in range(65, 91):
    letters2 += chr(n) + " "
print(letters2)
print(letters2[::-1].split(" ",5)) # string.split(separator, number of separations)

4. 大文字・小文字に関連する方法

str = "My name in Zyj hello world"
# capitalize() Only the first initial of the word is capitalized, the rest is lowercase
print(str.capitalize())
# My name in zyj hello world
# lower() converts letters to lowercase
print(str.lower())
# my name in zyj hello world
# upper() converts letters to upper case
print(str.upper())
# MY NAME IN ZYJ HELLO WORLD
# title() capitalize the first letter of each word, lowercase the rest
print(str.title())
# My Name In Zyj Hello World
# islower() isupper() istitle() Determines if the string is in format
print(str.isupper())
# False

5. 文字列を検索する

str1 = "Myaa namess inddaa Zyjcc helloxx worldbb"
# 1.count.py searches for the number of times a particular string exists
print(str1.count("aa"))
# 2. find string str.find(character or string , start subscript, end subscript) Returns the subscript number when the string was first found
# find() method does not find the substring will return -1
str2 = "My name in Zyj hello world My name in Zyj hello world"
print(str2.find("in", )) # Find the substring in, starting with subscript number 0
print(str2.find("in", 9)) # Find the substring in, starting with subscript number 9
# 3. str.index(character or string, start subscript, end subscript) return the specified string subscript value
print(str2.index("name"))
# index differs from find, index() will return an error if it fails to find, find() will return -1 value
# 4. startswith(character or string, start subscript, end subscript) Determine whether the beginning character of the string contains subcharacters
str3 = "My name in Zyj hello world My name in Zyj hello world"
print(str3.startswith("name", 3)) # True
# 5. str.endswith(character or string , start subscript, end subscript) determine whether the end of the string contains subcharacters
print(str3.endswith("world")) # True

要約

この記事があなたのお役に立ち、Script Houseの他のコンテンツにもっと注目していただけることを願っています。