Hàm str() trong trăn

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về chuỗi phương thức Find [] Python với sự trợ giúp của các ví dụ

Phương thức

quote = 'Let it be, let it be, let it be'

# first occurance of 'let it'[case sensitive]

result = quote.find['let it']

print["Substring 'let it':", result] # find returns -1 if substring not found

result = quote.find['small']

print["Substring 'small ':", result] # How to use find[]

if [quote.find['be,'] != -1]:

print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
6 trả về chỉ số xuất hiện đầu tiên của chuỗi con [nếu tìm thấy]. Nếu không tìm thấy, nó sẽ trả về -1. -1

Thí dụ

message = 'Python is a fun programming language'

# check the index of 'fun' print[message.find['fun']]

# Output: 12

find[] cú pháp

Cú pháp của phương thức

quote = 'Let it be, let it be, let it be'

# first occurance of 'let it'[case sensitive]

result = quote.find['let it']

print["Substring 'let it':", result] # find returns -1 if substring not found

result = quote.find['small']

print["Substring 'small ':", result] # How to use find[]

if [quote.find['be,'] != -1]:

print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
6 là

str.find[sub[, start[, end]] ]

find[] tham số

Phương thức

quote = 'Let it be, let it be, let it be'

# first occurance of 'let it'[case sensitive]

result = quote.find['let it']

print["Substring 'let it':", result] # find returns -1 if substring not found

result = quote.find['small']

print["Substring 'small ':", result] # How to use find[]

if [quote.find['be,'] != -1]:

print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
6 mất tối đa ba tham số

  • Sub - Đó là phần phụ được tìm kiếm trong chuỗi str. - Là xâu con cần tìm trong xâu str
  • Bắt đầu và kết thúc [Tùy chọn] - Phạm vi
    str.find[sub[, start[, end]] ]
    7 trong đó được tìm kiếm. và kết thúc [tùy chọn] - Phạm vi
    str.find[sub[, start[, end]] ]
    7 trong đó chuỗi con được tìm kiếm

find[] return value

Phương thức

quote = 'Let it be, let it be, let it be'

# first occurance of 'let it'[case sensitive]

result = quote.find['let it']

print["Substring 'let it':", result] # find returns -1 if substring not found

result = quote.find['small']

print["Substring 'small ':", result] # How to use find[]

if [quote.find['be,'] != -1]:

print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
6 trả về giá trị nguyên

  • Nếu chuỗi con tồn tại bên trong chuỗi, nó sẽ trả về chỉ số xuất hiện đầu tiên của chuỗi con
  • Nếu một chuỗi con không tồn tại bên trong chuỗi, nó sẽ trả về -1. -1

Work of method find[]

Hoạt động của các phương thức Find[] và RFind[] của Python String[]

Ví dụ 1. find [] không có đối số bắt đầu và kết thúc

quote = 'Let it be, let it be, let it be'

# first occurance of 'let it'[case sensitive]

result = quote.find['let it']

print["Substring 'let it':", result] # find returns -1 if substring not found

result = quote.find['small']

print["Substring 'small ':", result] # How to use find[]

if [quote.find['be,'] != -1]:

print["Contains substring 'be,'"] else: print["Doesn't contain substring"]

đầu ra

Substring 'let it': 11
Substring 'small ': -1
Contains substring 'be,'

Ví dụ 2. find[] with the number of started and end

message = 'Python is a fun programming language'

# check the index of 'fun' print[message.find['fun']]

# Output: 12
0

đầu ra

message = 'Python is a fun programming language'

# check the index of 'fun' print[message.find['fun']]

# Output: 12
1

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc trả về chỉ số thấp nhất hoặc lần xuất hiện đầu tiên của chuỗi con nếu nó được tìm thấy trong một chuỗi đã cho. Nếu không tìm thấy thì trả về -1

    Bàn luậnstr_obj. tìm [phụ, bắt đầu, kết thúc]

    Thông số.   

    • Phương thức Find [] chuỗi Python trả về chỉ số thấp nhất hoặc lần xuất hiện đầu tiên của chuỗi con nếu nó được tìm thấy trong chuỗi định dạng nhất. Nếu nó không được tìm thấy, thì nó sẽ trả về -1. Xâu con cần tìm trong xâu đã cho.  
    • cú pháp. str_obj. find [phụ, bắt đầu, kết thúc] Vị trí bắt đầu mà chuỗi con cần kiểm tra trong chuỗi.  
    • phụ. Subring cần được tìm kiếm trong chuỗi đã chọn.   . Nó được loại trừ trong khi kiểm tra.  

    Start [Tùy chọn]. Vị trí bắt đầu trong đó cần kiểm tra chuỗi con trong chuỗi.   . Nếu không tìm thấy thì trả về -1

    Kết thúc [Tùy chọn]. Vị trí kết thúc là mục cuối cùng của giá trị cuối cùng cho phạm vi được chỉ định. Nó được loại trừ trong khi kiểm tra.  

    Python3

    Trả về.   . Nếu nó không tìm thấy thì nó sẽ trả về -1

    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    2
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    3
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    4
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    5

    đầu ra

    str.find[sub[, start[, end]] ]
    4

    Ghi chú

    1. Ví dụ Phương thức Find [] Phương thức Python String []
    2. str.find[sub[, start[, end]] ]
      9
      quote = 'Let it be, let it be, let it be'
      
      # first occurance of 'let it'[case sensitive]
      

      result = quote.find['let it']

      print["Substring 'let it':", result] # find returns -1 if substring not found

      result = quote.find['small']

      print["Substring 'small ':", result] # How to use find[]

      if [quote.find['be,'] != -1]:

      print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
      0
      quote = 'Let it be, let it be, let it be'
      
      # first occurance of 'let it'[case sensitive]
      

      result = quote.find['let it']

      print["Substring 'let it':", result] # find returns -1 if substring not found

      result = quote.find['small']

      print["Substring 'small ':", result] # How to use find[]

      if [quote.find['be,'] != -1]:

      print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
      1

    Nếu các mục bắt đầu và kết thúc không được cung cấp thì theo mặc định, bạn sẽ mất 0 và độ dài-1 khi các mục bắt đầu và kết thúc trong đó các mục kết thúc không được bao gồm trong quá trình tìm kiếm

    Python3

    Trả về.   . Nếu nó không tìm thấy thì nó sẽ trả về -1

    Ví dụ Phương thức Find [] Phương thức Python String []

    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    2______95
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    6
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    7
    str.find[sub[, start[, end]] ]
    9
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    0
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    1
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    2
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    5
    message = 'Python is a fun programming language'
    
    

    # check the index of 'fun' print[message.find['fun']]

    # Output: 12
    05
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    7

    Nếu các mục bắt đầu và kết thúc không được cung cấp thì theo mặc định, bạn sẽ mất 0 và độ dài-1 khi các mục bắt đầu và kết thúc trong đó các mục kết thúc không được bao gồm trong quá trình tìm kiếm

    message = 'Python is a fun programming language'
    
    

    # check the index of 'fun' print[message.find['fun']]

    # Output: 12
    15
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    2
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    5
    message = 'Python is a fun programming language'
    
    

    # check the index of 'fun' print[message.find['fun']]

    # Output: 12
    18
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    3
    str.find[sub[, start[, end]] ]
    40
    message = 'Python is a fun programming language'
    
    

    # check the index of 'fun' print[message.find['fun']]

    # Output: 12
    14
    message = 'Python is a fun programming language'
    
    

    # check the index of 'fun' print[message.find['fun']]

    # Output: 12
    15
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    2
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    5
    str.find[sub[, start[, end]] ]
    45
    Substring 'let it': 11
    Substring 'small ': -1
    Contains substring 'be,'
    3

    đầu ra

    str.find[sub[, start[, end]] ]
    1

    Phương thức find [] tương tự như index []. Sự khác biệt duy nhất là tìm kiếm [] return -1 nếu không tìm thấy chuỗi được tìm kiếm và index [] ném một ngoại lệ trong trường hợp này

    Ví dụ 1. find [] không có đối số bắt đầu và kết thúc

    Python3

    Trả về.   . Nếu nó không tìm thấy thì nó sẽ trả về -1

    str.find[sub[, start[, end]] ]
    22
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    3____312
    str.find[sub[, start[, end]] ]
    13
    str.find[sub[, start[, end]] ]
    14
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    5

    Ví dụ Phương thức Find [] Phương thức Python String []

    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    2
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    3
    str.find[sub[, start[, end]] ]
    04
    str.find[sub[, start[, end]] ]
    13
    str.find[sub[, start[, end]] ]
    06
    str.find[sub[, start[, end]] ]
    13
    str.find[sub[, start[, end]] ]
    08
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    5
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    2
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    3
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    62
    str.find[sub[, start[, end]] ]
    13
    str.find[sub[, start[, end]] ]
    06
    str.find[sub[, start[, end]] ]
    13
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    66
    quote = 'Let it be, let it be, let it be'
    
    # first occurance of 'let it'[case sensitive]
    

    result = quote.find['let it']

    print["Substring 'let it':", result] # find returns -1 if substring not found

    result = quote.find['small']

    print["Substring 'small ':", result] # How to use find[]

    if [quote.find['be,'] != -1]:

    print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
    5

    đầu ra

    str.find[sub[, start[, end]] ]
    0

    Giải trình

    • str.find[sub[, start[, end]] ]
      9
      quote = 'Let it be, let it be, let it be'
      
      # first occurance of 'let it'[case sensitive]
      

      result = quote.find['let it']

      print["Substring 'let it':", result] # find returns -1 if substring not found

      result = quote.find['small']

      print["Substring 'small ':", result] # How to use find[]

      if [quote.find['be,'] != -1]:

      print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
      0
      quote = 'Let it be, let it be, let it be'
      
      # first occurance of 'let it'[case sensitive]
      

      result = quote.find['let it']

      print["Substring 'let it':", result] # find returns -1 if substring not found

      result = quote.find['small']

      print["Substring 'small ':", result] # How to use find[]

      if [quote.find['be,'] != -1]:

      print["Contains substring 'be,'"] else: print["Doesn't contain substring"]
      1
    • Trong tuyên bố thứ hai, giá trị bắt đầu được đưa ra là 2 và phần phụ được đưa ra là 'chuyên gia máy tính', do đó, vị trí chỉ số của 'Geek' là 10 nhưng giá trị cuối cùng bị loại trừ
    • Trong câu lệnh thứ ba, giá trị bắt đầu = 4, giá trị cuối = 10 và chuỗi con = 'g' được đưa ra, vị trí chỉ mục từ 4 sẽ được kiểm tra vì chuỗi con đã cho ở vị trí 10, được
    • Trong câu lệnh thứ tư, giá trị bắt đầu = 4, giá trị cuối = 11 và chuỗi con = 'cho' được đưa ra, vị trí chỉ mục từ 4 đến 11 sẽ được kiểm tra đầu ra thu được

    Đang tìm một chuỗi phương thức?

    Find [] là một chuỗi công thức tìm thấy một chuỗi con trong một chuỗi và trả về chỉ mục của chuỗi con. Phương thức Find [] chấp nhận ba tham số. Sub là phần phụ cần tìm trong str. tìm chuỗi con trong một chuỗi và trả về chỉ mục của chuỗi con. Phương thức find[] chấp nhận ba tham số. sub là chuỗi con cần tìm trong str.

    Làm cách nào để tìm một chuỗi trong một chuỗi python?

    Sử dụng toán tử 'IN'. Toán tử trong cách dễ nhất và pythonic để kiểm tra xem chuỗi python có chứa chất nền không. Các nhà khai thác thành viên trong và không phải là thành viên, họ đưa ra hai lập luận và đánh giá xem một người là thành viên của người kia. Họ trả lại một giá trị boolean

    Chủ Đề