程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning -- Basic Python syntax (3): data type -- string

編輯:Python

python The most basic data types in are as follows :

Number( Numbers )

String( character string )

List( list )

Tuple( Tuples )

Dictionary( Dictionaries )

One 、 String creation

String type ’str’ Is the most commonly used data type , You can use quotation marks ( Single or double quotation marks ) To create a string . There are two points to note :

1. Single quotation marks and double quotation marks are essentially the same . But when the string contains single quotation marks , If you use a single quoted string , As a result, the single quotation mark in the string cannot be distinguished from the single quotation mark in the string , So use the escape string . If you use a double quoted string , Write single quotation marks directly in the string . Such as :

        ‘abc”dd”abc’

        “’acc’d’12”

2. A three quote string can consist of multiple lines , Single quote or double quote strings do not , When you need to use large, multi line strings , have access to . Such as :

        ‘’’

         Multiple string references

        ‘’’

     stay python In the program , Characters in a string can include numbers 、 Letter 、 Chinese characters 、 Special symbols , And some invisible control characters , As a newline 、 Box drawings, etc .

Two 、 The subscript of a string str[beg:end]

str = 'hello world'
print(str[1])  # e Subscript from 0 Start
print(str[10]) # d
print(str[-1]) # d Take the last value
print(len(str))# 11
print(str[12]) # Report errors Transboundary , Out of range 

3、 ... and 、 Slice of string :slice(start, stop[, step])

str = 'hello world'
print(str [6:11:])  # world The range is semi closed and semi open [6:10)
print(str [::])  # hello world Take the whole string
print(str [::2]) # hlowrd   Value based on step size , Step cannot be 0, Floating point numbers are not allowed
print(str [::-1])# dlrow olleh Output in reverse order
print(str [6:12:]) #world When taking a single character , If the index is out of range, an error will be reported . No error will be reported when slicing .

Four 、 String formatting :# %s—— character string ,%d—— Integers ,%f—— Floating point numbers

# String formatting
name = 'hansen'
age = 20
# % Data types need to be considered
print("%s The age is %d" %(name, age))#hansen The age is 20
print("{} The age is {}".format(name, age)) #hansen The age is 20   Order to be considered
print("{1} The age is {0}".format(age, name)) #hansen The age is 20 There is no need to consider the order , Consider indexing
print(f"{name} The age is {age}") #hansen The age is 20

5、 ... and 、 Common methods of string

S.find(sub) --> Returns the smallest index of the element S.index(sub) --> Returns the smallest index of the element

S.replace(old, new[, count]) --> Replace

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

S.startswith(prefix[, start[, end]]) --> Determine whether the string starts with a prefix , Return to bool value .

S.endswith(suffix[, start[, end]]) --> Determine whether a string ends with a suffix , Return to bool value .

S.lower() --> Convert all strings to lowercase

S.upper() --> Convert all strings to uppercase

S.strip([chars]) --> By default, the characters around the string are removed , Default is space

S.isalpha() --> Determine whether the string is all alphabetic , The return is bool value

S.isdigit() --> Determine whether the string is all numbers , The return is bool value

S.isalnum() --> Judge whether the string is all numbers or letters , There are no special characters , The return is bool value

S.join(iterable) --> The elements in the sequence are concatenated with the specified characters to produce a new string


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved