zoukankan      html  css  js  c++  java
  • Python | 查找给定字符串中字符的位置

     
     

    给定一个字符串和一个字符,您的任务是找到字符在字符串中的第一个位置。这些类型的问题是非常有竞争力的编程,您需要在其中定位字符在字符串中的位置。

    让我们讨论一些解决问题的方法。

    方法1:Using Naive Method

    # Python3 code to demonstrate 
    # to find the first position of the character 
    # in a given string 
    
    # Initialising string 
    ini_string = 'abcdef'
    
    # Character to find 
    c = "b"
    # printing initial string and character 
    print ("initial_string : ", ini_string, "
    character_to_find : ", c) 
    
    # Using Naive Method 
    res = None
    for i in range(0, len(ini_string)): 
        if ini_string[i] == c: 
            res = i + 1
            break
        
    if res == None: 
        print ("No such charater available in string") 
    else: 
        print ("Character {} is present at {}".format(c, str(res))) 
    输出:
    initial_string :  abcdef 
    character_to_find :  b
    Character b is present at 2

    方法2:Using find

    如果字符不存在,则此方法返回-1

    # Python3 code to demonstrate 
    # to find first position of character 
    # in a given string 
    
    # Initialising string 
    ini_string = 'abcdef'
    ini_string2 = 'xyze'
    
    # Character to find 
    c = "b"
    # printing initial string and character 
    print ("initial_strings : ", ini_string, " ", 
            ini_string2, "
    character_to_find : ", c) 
    
    # Using find Method 
    res1 = ini_string.find(c) 
    res2 = ini_string2.find(c) 
        
    if res1 == -1: 
        print ("No such charater available in string {}".format( 
                                                    ini_string)) 
    else: 
        print ("Character {} in string {} is present at {}".format( 
                                    c, ini_string, str(res1 + 1))) 
        
    if res2 == -1: 
        print ("No such charater available in string {}".format( 
                                                ini_string2)) 
    else: 
        print ("Character {} in string {} is present at {}".format( 
                                    c, ini_string2, str(res2 + 1))) 

    输出:

    initial_strings :  abcdef   xyze 
    character_to_find :  b
    Character b in string abcdef is present at 2
    No such charater available in string xyze

    方法#3:Using index()

    如果字符不存在,则此方法引发ValueError

    # Python3 code to demonstrate 
    # to find first position of character 
    # in a given string 
    
    # Initialising string 
    ini_string1 = 'xyze'
    
    # Character to find 
    c = "b"
    # printing initial string and character 
    print ("initial_strings : ", ini_string1, 
                "
    character_to_find : ", c) 
    
    # Using index Method 
    try: 
        res = ini_string1.index(c) 
        print ("Character {} in string {} is present at {}".format( 
                                    c, ini_string1, str(res + 1))) 
    except ValueError as e: 
        print ("No such charater available in string {}".format(ini_string1)) 
    输出:
    initial_strings :  xyze 
    character_to_find :  b
    No such charater available in string xyze
  • 相关阅读:
    Smarty数据、模版创建指引
    做了个google工具栏的饭否按钮
    php程序调试(远程调试,firephp)
    Linux shell编程与Makefile的一些记录(等待更新)
    My MSSQL Tips
    Castle.ActiveRecord中Save与SaveAndFlush的性能差别
    Linux Basic Approach (My Notes)
    去青城后山玩了一天
    Infopath 2007 使用实践(占位)
    Delphi 6
  • 原文地址:https://www.cnblogs.com/a00ium/p/13875119.html
Copyright © 2011-2022 走看看