输出给定子串在字符串中出现的所有位置,如无此串,显示“no this string!” Input 输入两个字符串str1,str2。第一个字符串str1为源字符串,str2为待查找子串 。 Output 输出所有子串在源字符串中出现的位置,如果有多个,中间用分号隔开。如果不存在则输出“no this string!”。
时间: 2023-11-27 13:55:42 浏览: 71
以下是 Python 代码实现:
```python
str1 = input("请输入源字符串:")
str2 = input("请输入待查找子串:")
if str2 not in str1:
print("no this string!")
else:
positions = []
start = 0
while True:
position = str1.find(str2, start)
if position == -1:
break
positions.append(str(position))
start = position + 1
print(";".join(positions))
```
代码思路:
首先输入源字符串和待查找子串,如果待查找子串不在源字符串中,则输出“no this string!”;否则,我们使用 `find()` 方法查找待查找子串在源字符串中的位置,如果找到了,则将该位置添加到位置列表中,并将查找的起始位置移动到该位置之后,继续进行查找,直到无法找到为止。最后,我们将列表中的位置用分号拼接起来输出。
相关问题
定义一个函数完成两个字符串的减法,例如,'abcabcde'-'ab'表示从字符串'abcabcde'中删除全部'ab',结果为'ccde'。字符串的减法表达式从键盘输入。Python代码
### 回答1:
def subtract_string(str1, str2):
return str1.replace(str2, "")
str1 = input("请输入待操作的字符串:")
str2 = input("请输入要删除的字符串:")
result = subtract_string(str1, str2)
print(result)
### 回答2:
可以定义一个函数来完成两个字符串的减法操作。
首先,我们可以从键盘输入两个字符串,分别表示被减数和减数,假设分别为s1和s2。
然后,可以使用Python内置的replace()方法将s2在s1中的所有出现替换为空字符串,即删除s1中的所有s2。最后返回结果即可。
下面是实现这个函数的Python代码:
```python
def subtract_strings():
s1 = input('请输入被减数:')
s2 = input('请输入减数:')
result = s1.replace(s2, '')
print('减法结果为:', result)
subtract_strings()
```
这样,就可以通过调用`subtract_strings()`函数来实现两个字符串的减法。输入一个被减数和一个减数后,函数会打印出减法结果。例如,对于输入的'abcabcde'和'ab'来说,函数会打印出'ccde'作为结果。
### 回答3:
可以通过遍历字符串的方式实现字符串的减法。具体步骤如下:
1. 定义一个函数`subtract_str(str1, str2)`,其中`str1`为原始字符串,`str2`为要删除的子字符串。
2. 首先将`str1`和`str2`转换为列表类型,方便后续的元素删除操作。
3. 遍历`str2`的每个字符,在`str1`中查找该字符的位置,并将该位置的元素从`str1`中删除。
4. 返回删除后的`str1`,并将其转换为字符串类型。
5. 在主程序中,通过键盘输入获取需要进行减法运算的字符串和要删除的子字符串。
6. 调用`subtract_str()`函数,并打印最终的结果。
以下是Python代码示例:
```python
def subtract_str(str1, str2):
str1_list = list(str1)
str2_list = list(str2)
for char in str2_list:
if char in str1_list:
str1_list.remove(char)
result = ''.join(str1_list)
return result
if __name__ == '__main__':
str1 = input("请输入要进行减法运算的字符串:")
str2 = input("请输入要删除的子字符串:")
result = subtract_str(str1, str2)
print("处理结果为:", result)
```
通过上述Python代码,即可实现从字符串中删除指定子字符串的减法操作。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)