将两个任意字符串连接起来并输出
时间: 2023-06-05 17:08:37 浏览: 50
好的,将两个任意字符串连接起来并输出可以使用字符串拼接的方法,例如在 Python 中可以使用加号 "+" 连接两个字符串,示例代码如下:
```
str1 = "Hello"
str2 = " World!"
result = str1 + str2
print(result)
```
输出结果为:
```
Hello World!
```
相关问题
C语言将两个任意字符串连接起来并输出
可以使用 strcat 函数将两个字符串连接起来并输出,示例代码如下:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello ";
char str2[] = "world!";
strcat(str1, str2);
printf("%s", str1);
return 0;
}
```
输出结果为:"Hello world!"
编写一个函数,将两个字符串连接起来
以下是 Python 代码实现:
```python
def concat_strings(str1, str2):
return str1 + str2
```
该函数接收两个字符串作为参数,使用加号运算符将它们连接起来,并返回连接后的字符串。可以在调用该函数时传入任意两个字符串,如下所示:
```python
print(concat_strings("Hello", "World")) # 输出 "HelloWorld"
print(concat_strings("Python", " is awesome!")) # 输出 "Python is awesome!"
```