相关疑难解决方法(0)

Python有一个字符串'contains'子串方法吗?

我正在寻找Python中的一个string.containsstring.indexof方法.

我想要做:

if not somestring.contains("blah"):
   continue
Run Code Online (Sandbox Code Playgroud)

python string substring contains

3601
推荐指数
10
解决办法
386万
查看次数

如果存在字符则拆分字符串,否则不拆分

我有一个string喜欢下面的python

testing_abc
Run Code Online (Sandbox Code Playgroud)

我想根据元素分割字符串_并提取2元素

我已经做了如下

split_string = string.split('_')[1]
Run Code Online (Sandbox Code Playgroud)

我得到了预期的正确输出

abc
Run Code Online (Sandbox Code Playgroud)

现在我希望它适用于以下字符串

1) xyz
Run Code Online (Sandbox Code Playgroud)

当我使用

split_string = string.split('_')[1]
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

list index out of range
Run Code Online (Sandbox Code Playgroud)

我想要的预期输出是xyz

2) testing_abc_bbc
Run Code Online (Sandbox Code Playgroud)

当我使用

split_string = string.split('_')[1]
Run Code Online (Sandbox Code Playgroud)

我得到abc的输出

我想要的预期输出是abc_bbc

基本上我想要的是

1) If string contains `_` then print everything after the first `_` as variable
2) If string doesn't contain `_` then print the string as variable
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现我想要的

python python-3.x

4
推荐指数
1
解决办法
1955
查看次数

Python:从字符串中打印特定字符

如何在Python中从字符串中打印特定字符?我仍在学习,现在尝试制作类似hangman的程序。这个想法是用户输入一个字符,如果它在单词中,则该单词将以所有未发现的字母打印为“-”。

我并不是在寻求一种使我的整个项目的想法/代码更好的方法,而只是要求一种打印字符串的特定字符的方法。

python printing string

3
推荐指数
1
解决办法
3万
查看次数

Python 3.x短版本的代码

有没有办法检查字符串中的字符?感谢:D

check = input("Put in the letter: ")
word = "word"

if(check == word[0]):
    print(check)
if(check == word[1]):
    print(check)
if(check == word[2]):
    print(check)
if(check == word[3]):
    print(check)
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
76
查看次数

如何检查字符串是否包含特殊字母?

我正在构建一个程序,它以字符串形式获取密码并通过几个因素检查其强度。我想检查输入的字符串是否包含特殊字符(如 %、$、# 等),但到目前为止我无法弄清楚。这样做的最佳方法是什么?

编辑:我不是在寻找特定的角色。我需要搜索字符串以查找它是否具有某种非字母、非数字字符。

编辑 2 :我想在没有循环的情况下进行。

python python-2.7

-1
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×5

string ×2

contains ×1

printing ×1

python-2.7 ×1

python-3.x ×1

substring ×1