python实现字符串交换:输入两个长度为4的字符串,交换这两个字符串的前两个字符后输出
时间: 2023-12-30 17:04:11 浏览: 205
可以使用Python中的字符串切片(slice)操作实现字符串交换。
代码如下:
```
str1 = input("请输入第一个字符串:")
str2 = input("请输入第二个字符串:")
# 交换字符串前两个字符
new_str1 = str2[:2] + str1[2:]
new_str2 = str1[:2] + str2[2:]
print("交换后的字符串为:")
print(new_str1)
print(new_str2)
```
运行示例:
```
请输入第一个字符串:abcd
请输入第二个字符串:efgh
交换后的字符串为:
efcd
abgh
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](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)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)