美文网首页Python🐍
python - string(1)

python - string(1)

作者: Pingouin | 来源:发表于2020-08-31 23:45 被阅读0次

strings

  • we can get single character in a string by using an index specified in square brackets.
  • the index value must be an integer and starts at zero
  • the index value can be an expression that is computed
  • you will get a python error if attemplt to index beyond the range
# give a length of string 
len(string)

loop through strings

fruit = 'banana'
index = 0
while index < len(fruit):
  letter = fruit[index]
  print(index, letter)
  index = index + 1
fruit = 'banana'
for letter in fruit: 
  print(letter)
# for loop looks shorter

loop and counting

word = 'banana'
count = 0
for letter in word:
  if letter == 'a':
    count += 1
print(count)

- look deeper into 'in'

  • The iteration variable 'iterates' through the sequence (ordered set)
  • the block (body) of code is executed once for each value in the sequence
  • the iteration variable moves through all of the values in the sequence

more string operation

1. slicing strings

s = 'Monty Python'
print(s[0:4]) # the second number, here is 4, is one beyond the end of the slice 
# "up to but not including"
print(s[6:7])
print(s[6:20])
print(s[:])
  • if the second number is beyond the end of the string, it stops at the end.
  • if weleave off the first number or the last number of the slice, it is assumed to be the beginning or end of the string respectively.

2. string concatenation

  • when the + operator is applied to the strings, it means concatenation.
a = 'hello'
b = a + 'there'
print(b)
c = a + ' ' + 'there'
print(c)
  • use in as a logical operator
    The in expression is a logical expression that returns True or False and can be used in an if statement.
fruit = 'banana'
'n' in fruit 
# >>> True
if 'a' in fruit:
  print('Found it')

3. string library

string.lower()
print('HI.There'.upper())
type('string1') #返回class
dir('string1') #返回所有该数据类型可用的内部的函数

4. search a string, make everything upper case

  • we use find() function to search for a substring within another string.
  • find() finds the first occurrence of the substring.
  • If the substring is not found, find() returns -1
fruit = 'banana'
pos = fruit.find('ana') # 注意find函数找的是第一个
print(pos)
aa = fruit.find('z')
print(aa)

5. search and replace

  • The replace() function is like a "search and replace" operation in a word processor
  • it replaces all occurrences of the search string with the replacement string.
greet = 'hi ke? ke!'
nstr = greet.replace('ke', 'daidai')
print(nstr)

6. stripping whitespace

greet = '   hi ke.     '
greet.lstrip()
greet.rstrip()
greet.strip() # remove both beginning and ending whitespace, not the middle!!!

can be tab/newline

7. prefixes

line = 'what is your name?'
line.startswith('what')

8.parsing and extracting

data = 'From ke.zhang@ugent.be sat Jan 5 09:00:10 2020'
atpos = data.find('@')
print(atpos)
sppos = data.find(' ',atpos) # 从atpos之后的空格
print(sppos)
host = data[atpos+1:sppos]
print(host)

练习

  • The isalpha() method returns True if all the characters are alphabet letters (a-z).
  • Checking whether two strings contain the same characters (that are not necessarily in the same order) can easily be done by sorting both strings and comparing the sorted results with each other. Sorting two strings in Python can be done using the sorted-function. This return into a list

相关文章

网友评论

    本文标题:python - string(1)

    本文链接:https://www.haomeiwen.com/subject/ohrhsktx.html