Python 程序:寻找字符串中的字符的第一次出现

写一个 Python 程序,用一个实际的例子找到字符串中的字符的第一次出现。

python 程序查找字符串中的字符的第一次出现示例 1

这个 python 程序允许用户输入字符串和字符。

请参考弦文章了解蟒弦的一切。

# Python Program to check First Occurrence of a Character in a String

string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")

flag = 0
for i in range(len(string)):
    if(string[i] == char):
        flag = 1
        break

if(flag == 0):
    print("Sorry! We haven't found the Search Character in this string ")
else:
    print("The first Occurrence of ", char, " is Found at Position " , i + 1)

Python 字符串输出中字符的第一次出现

Please enter your own String : hello world
Please enter your own Character : l
The first Occurrence of  l  is Found at Position  3

这里,我们使用 For Loop 来迭代字符串中的每个字符。在 For 循环中,我们使用 If 语句来检查 str1 字符串中的任何字符是否等于字符 ch。如果为真,则标志= 1,并执行中断语句。

string = hello world
ch = l
flag = 0

对于循环第一次迭代:对于范围(11)中的 I,如果(字符串[i] == char)
如果(h = = l)–条件为假。

第二次迭代:如果(e = = l)–条件为假,范围(11)
中的 1。

第三次迭代:对于范围(11)中的 2,如果(str[2] == ch) = >如果(l = = l)–条件为真。

标志= 1,中断语句退出循环。接下来,我们使用 If Else 语句检查标志值是否等于 0。这里,条件为假。所以,在执行的 else 块内打印。

Python 程序查找字符串中的字符的第一次出现示例 2

这个 Python 字符的第一次出现程序与上面的相同。然而,我们只是将 For 循环替换为 While 循环。

# Python Program to check First Occurrence of a Character in a String

string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
i = 0
flag = 0

while(i < len(string)):
    if(string[i] == char):
        flag = 1
        break
    i = i + 1

if(flag == 0):
    print("Sorry! We haven't found the Search Character in this string ")
else:
    print("The first Occurrence of ", char, " is Found at Position " , i + 1)

Python 字符串输出中字符的第一次出现

Please enter your own String : python programming
Please enter your own Character : o
The first Occurrence of  o  is Found at Position  5

获取字符串中字符的第一次出现的 Python 程序示例 3

这个 python 程序查找字符串中的字符的第一次出现与第一个例子相同。然而,这一次,我们使用了函数概念来分离逻辑。

# Python Program to check First Occurrence of a Character in a String

def first_Occurrence(char, string):
    for i in range(len(string)):
        if(string[i] == char):
            return i
    return -1

str1 = input("Please enter your own String : ")
ch = input("Please enter your own Character : ")

flag =  first_Occurrence(ch, str1)
if(flag == -1):
    print("Sorry! We haven't found the Search Character in this string ")
else:
    print("The first Occurrence of ", ch, " is Found at Position " , flag + 1)

原创文章,作者:WAQ7X,如若转载,请注明出处:https://www.506064.com/n/129246.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
WAQ7XWAQ7X
上一篇 2024-10-03 23:26
下一篇 2024-10-03 23:26

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • 英语年龄用连字符号(Hyphenation for English Age)

    英语年龄通常使用连字符号表示,比如 “five-year-old boy”。本文将从多个方面探讨英语年龄的连字符使用问题。 一、英语年龄的表达方式 英语中表…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29

发表回复

登录后才能评论