python创建字符串的代码
时间: 2023-10-28 20:06:15 浏览: 27
可以通过以下代码来创建字符串:
```python
str1 = "Hello World!" # 使用双引号创建字符串
str2 = 'Hello World!' # 使用单引号创建字符串
str3 = """Hello
World!""" # 使用三个双引号创建多行字符串
str4 = '''Hello
World!''' # 使用三个单引号创建多行字符串
```
其中,`str1` 和 `str2` 都是使用引号(单引号或双引号)来创建的普通字符串,`str3` 和 `str4` 是使用三个引号来创建的多行字符串。
相关问题
python创建二维字符串
你可以使用Python的列表来创建二维字符串。下面是一个简单的例子:
```python
# 创建一个3x3的二维字符串数组
arr = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
# 输出整个数组
for row in arr:
print(' '.join(row))
```
这将输出以下内容:
```
a b c
d e f
g h i
```
你可以根据你的需要修改此代码。
Python 用字符串选择函数
在Python中,可以使用字符串来选择函数的一种常见方式是使用字典。你可以创建一个字典,将字符串作为键,将对应的函数作为值。然后,根据给定的字符串,在字典中查找对应的函数并调用它。
以下是一个示例代码:
```python
def func1():
print("这是函数1")
def func2():
print("这是函数2")
def func3():
print("这是函数3")
# 创建一个字典,将字符串与对应的函数关联起来
function_dict = {
"func1": func1,
"func2": func2,
"func3": func3
}
# 根据用户输入的字符串选择对应的函数并调用
user_input = input("请输入函数名:")
if user_input in function_dict:
selected_func = function_dict[user_input]
selected_func()
else:
print("无效的函数名")
```
在上面的示例中,我们定义了三个函数func1、func2和func3,并创建了一个字典function_dict来关联字符串与对应的函数。然后,根据用户输入的字符串选择对应的函数并调用。