Detailed Python data type - String

Detailed Python data type - String

First, the definition

String is an ordered set of characters, and for storing information showing a basic text, with '', "", ''' '''the contents contained in the intermediate string called

create:s = 'Hello world!'

Second, the characteristics

  • As defined character set from left to right in the order, from index 0 accessed sequentially and orderly.
  • You can slice
  • Immutable, strings are immutable and can not want to modify the list as an element, all modification operations on strings are quite a new data generated.
  • String of single and double quotes are unable to cancel the special character meaning, in quotation marks if you want to cancel all of the characters are of special significance, plus r in front of the quotes, such as name = r'p \ thf '

Third, the character string common operations

The method of the string are many, but some are not used, as shown in FIG.

#captalize,swapcase,title
name = 'kwan'
print(name.capitalize()) #首字母大写
print(name.swapcase()) #大小写翻转
msg='taibai say hi'
print(msg.title()) #每个单词的首字母大写

# 内同居中,总长度,空白处填充
a1 = 'kwan'
ret2 = a1.center(20,"*")  
print(ret2)


#寻找字符串中的元素是否存在
a4 = 'sdfjdkaiwan'
ret6 = a4.find("fjdk",1,6)
print(ret6)  # 返回的找到的元素的索引,如果找不到返回-1

ret61 = a4.index("fjdk",4,6)
print(ret61) # 返回的找到的元素的索引,找不到报错。

# 字符串每个字符之间插入符号,join
s1 = 'kwan'
s = '+'.join(s1)        # 结果为 'k+w+a+n'

# 清除字符串里左右两边的空格,strip()
s = '   Kwan   '
print(s.strip())         # 结果为 'Kwan'


# 判断字符串是否为纯数字,isdigit()
w = '437284'
print(w.isdigit())       # 结果为True

These used more than, not all methods are necessary to learn and master a few commonly used on the line, the other look on the line.

Guess you like

Origin www.cnblogs.com/Kwan-C/p/11461153.html