python判断字符串是否以**开头
时间: 2023-06-13 13:02:40 浏览: 51
可以使用字符串的 `startswith()` 方法来判断字符串是否以某个子串开头。例如,判断字符串 `s` 是否以子串 `prefix` 开头,可以用以下代码:
```
if s.startswith(prefix):
print("字符串以", prefix, "开头")
else:
print("字符串不以", prefix, "开头")
```
其中 `prefix` 为要判断的子串,如果字符串以该子串开头,`startswith()` 方法返回 `True`,否则返回 `False`。
相关问题
python判断字符串以*开头
可以使用字符串的startswith()方法来判断一个字符串是否以指定的字符或字符串开头,例如:
```
s = "*Hello, World!"
if s.startswith("*"):
print("字符串以*开头")
else:
print("字符串不以*开头")
```
输出结果为:
```
字符串以*开头
```
python 判断字符串以什么开头
你可以使用字符串的 startswith() 方法来判断一个字符串是否以某个字符或子串开头,例如:
```python
s = "hello world"
if s.startswith("hello"):
print("s starts with 'hello'")
```
输出:
```
s starts with 'hello'
```
你也可以传入一个元组作为参数,判断字符串是否以其中任意一个元素开头,例如:
```python
s = "hello world"
if s.startswith(("hello", "hi")):
print("s starts with 'hello' or 'hi'")
```
输出:
```
s starts with 'hello' or 'hi'
```