current position:Home>How IOS developers learn Python Programming 5 - data types 2

How IOS developers learn Python Programming 5 - data types 2

2022-01-31 09:15:22 Sissy's father

This is my participation 11 Yue Gengwen challenges 5 God , Check out the activity details :2021 The last time Gengwen challenge .

character string

The string is Python One of the most common data types in , Use Single quotation marks or Double quotes To create a string , Use Three quotes Create a multiline string .

The string is Immutable sequence data type , You can't modify the string itself directly , Just like the number type . So when we're going from "hello world" To get a letter out of it, you need to index it ( The default from the 0 Start ) Come and get it :

>>> s1 = "hello world"          
>>> s1[1]               
'e'
>>> s1[10]
'd'
>>> s1[-1]
'd'
>>> s1[11]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
 Copy code 

️ Be careful : Strings are immutable data types .

String slice (slice)

Input help(slice), View slice documentation :

slice(start, stop[, step])
 Copy code 
  • start The starting position , Default index from 0 Start
  • stop End position , Default last element
  • step step , The default is 1
  1. from "hello world" Take out world value : 
>>> s3 = "hello world"
>>> s3[6:10]            #  Be careful , Left closed right away 
'worl'
>>> s3[6:11]
'world'
>>> s3[6:]              #  If you don't write, you will get the last digit by default 
'world'
 Copy code 
  1. Output in reverse order : 
>>> s3[::-1]
'dlrow olleh'
 Copy code 
  1. Convert string to integer type : 
>>> int('1')
1
 Copy code 
  1. Convert integer type to string : 
>>> str(1)
'1'
 Copy code 
  1. String concatenation : 
print('1'+'2')  # 12
 Copy code 
  1. Formatted string :
    %s %d %f: 
    str.format(): 
    Abbreviation str.format: 
name = 'cat'
age = 18
print('%s Age %d'%(name,age))
 Copy code 
name = 'cat'
age = 18
print('{} Age {}'.format(name,18))
print('{1} Age {0}'.format(18,name))
 Copy code 
name = 'cat'
age = 18
print(f'{name} The age of {age}')
 Copy code 

String common methods

  1.  S.find(sub)  Returns the smallest index of the element : 
>>> s4 = 'hello python'
>>> s4.find('e')
1
>>> s4.find('o')    #  When there are multiple elements , Returns the minimum index 
4
>>> s4.find('c')    #  If it cannot be found, it is -1
-1
 Copy code 
  1.  S.index(sub) Returns the smallest index of the element , The method and s.find() It's the same function , But the only difference is when the element doesn't exist ,s.index() The method will report an error . So it is recommended to use s.find()
  2.  S.replace(old, new[, count]) Replace : 
>>> s5 = "hello python"
>>> s5.replace('l','a')     #  take 'l' Replace with 'a', All replacement 
'heaao python'

>>> s5
'hello python'              #  Be careful : The original string has not been changed 

>>> s5.replace('l','a',1)   #  Just replace one , Specifies the count Parameter is 1 that will do 
'healo python'
 Copy code 
  1.  S.split(sep=None) With sep To split the string , And return to the list .sep The default is None, The default split is space : 
>>> s6 = 'hello everyboby ye!'
>>> s6.split(' ')
['hello', 'everyboby', 'ye!']
 Copy code 
  1.  S.startswith(prefix[, start[, end]]) Determine whether the string starts with a prefix , Return to bool value : 
>>> s7 = "hello world"
>>> s7.startswith("he")
True
 Copy code 
  1.  S.endswith(suffix[, start[, end]]) Determine whether a string ends with a suffix , Return to bool value : 
>>> s7
'hello world'
>>> s7.endswith('ld')
True
 Copy code 
  1.  S.lower() Convert all strings to lowercase . 
  2.  S.upper() Convert all strings to uppercase . 
  3.  S.strip([chars]) By default, the spaces around the string are removed : 
>>> s8 = '  hello world   '
>>> s8.strip()
'hello world'
 Copy code 
  1.  S.isalpha()  Determine whether the string is all alphabetic , The return is bool value . 
  2.  S.isdigit() Determine whether the string is all numbers , The return is bool value . 
  3.  S.isalnum() Judge whether the string is all numbers or letters , There are no special characters , The return is bool value . 
  4.  S.join(iterable) The elements in the sequence are concatenated with the specified characters to produce a new string .

copyright notice
author[Sissy's father],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310915190375.html

Random recommended