The string type consists of multiple characters that can be used as a whole, or any part of a string.
function Len () returns the length of the string
' www.baidu.com '>>> len (address)13
View CodeTo traverse a string with a for statement
Starting with the first character, reading the characters sequentially, and then doing the corresponding processing until the last character, this process is called traversal.
for inch address: ... Print Char
View CodeString fragment
Part of the string is called a fragment, and the operator [n:m] returns part of the string, starting at the nth character to the end of the first m character, including Nth, but excluding the first m. The write-only m means starting at index 0, and write only n for N to the last character.
" Linux Unix FreeBSD " print os[0:5]linuxprint os[6:10]unixprint os[:5]linux Print os[11:]freebsd
View Code
If we modify one of these characters, the best thing to do is to take part of the string and add or replace the string to form a new string:
" Use python! ">>> new_s = s[0:3]+" P"+s[5:]print new_suse python!
View Code
From a string, look for the existence of the given character:
Python Learning Notes: Strings