String 1, the definition of a string
- strings are a string of characters and are data types that represent text in a programming language
- You can use a pair of double quotes
" or a pair of single quotes to define a string in Python ‘
- While you can use
\" or \‘ do a string escape, in real-world development:
- If you need to use
" the string internally, you can use the ‘ definition string
- If you need to use
‘ the string internally, you can use the " definition string
- You can use an index to get a character at the specified position in a string, starting with the 0 index Technique
- You can also use
for loops to iterate through every character in a string
Most programming languages are used " to define strings
str_test = "Hello Python" for i in str_test: print(i) H e l l o P y t h o n # 1,统计字符串长度 length = len(str_hello) print(length) 11 # 2,统计某一个小字符串出现的次数 frequency_a = str_hello.count("llo") print(frequency_a) 2 # 如果收搜一个不存在的字符串,则显次数为0 frequency_b = str_hello.count("abc") print(frequency_b) 0 # 3,某一个字符串出现的位置 location_a = str_hello.index("llo") print(location_a) 2 # 如果字符串不存在,则程序报错 location_b = str_hello.index("abc") print(location_b)
Common Operations for strings
ipython3define a string in, for examplestr_hello = "hello hello"
- Enter
str_hello. TAB the key to ipython3 prompt the string to use the following method :
In [1]: Str_hello = "Hello hello" in [2]: Str_hello. Str_hello.capitalize str_hello.expandtabs str_hello.isalpha str_hello.isnumeric str_hello.ljust Str_hel Lo.rfind str_hello.split str_hello.translate str_hello.casefold str_hello.find Str_hello. Isascii str_hello.isprintable str_hello.lower str_hello.rindex str_hello.splitlines str_hello.upper Str_hello.center Str_hello.format str_hello.isdecimal str_hello.isspace str_hello.lstrip s Tr_hello.rjust str_hello.startswith str_hello.zfill str_hello.count str_hello.format_map Str_hell O.isdigit str_hello.istitle Str_hello.maketrans str_hello.rpartition str_hello.strip str_hello.encod E str_hello.index str_hello.isidentifier str_hello.isupper str_hello.partition str_hello.rsplit Str_hello.swapcase Str_hello.endswith Str_hello.isalnum StR_hello.islower str_hello.join str_hello.replace Str_hello.rstrip str_hello.title
Tip: It's because Python has a built-in approach that allows for more flexible manipulation of strings at development time.
1, judging type
||||
Python Advanced Variable Type---string