已知两个字符串“qingjiao”,“student”,请将字符串按照顺序使用“-”拼接并打印。python代码
时间: 2023-11-12 16:05:43 浏览: 32
可以使用字符串的join方法,将两个字符串按照“-”拼接起来,然后打印即可。
代码如下:
```python
s1 = "qingjiao"
s2 = "student"
result = "-".join([s1, s2])
print(result)
```
输出结果为:
```
qingjiao-student
```
相关问题
.字符串拼接已知两个字符串“qingjiao“hongy日”,请将字符串按照顺序进行拼接并
字符串拼接是将多个字符串按照一定的顺序连接起来形成一个新的字符串。对于已知的两个字符串“qingjiao”和“hongy日”,我们可以按照顺序进行拼接。
首先,我们将字符串“qingjiao”后面直接拼接字符串“hongy日”,即可得到一个新的字符串。拼接的方法可以使用加号“+”,也可以使用字符串的拼接函数,如concat()。
代码如下:
```python
# 使用加号拼接字符串
str1 = "qingjiao"
str2 = "hongy日"
str3 = str1 + str2
# 使用字符串的拼接函数concat()
# str3 = str1.concat(str2)
print(str3)
```
运行结果为:"qingjiao hongy日",即先输出“qingjiao”,然后再输出“hongy日”。
以上是将两个字符串按照顺序拼接的方法。如果有更多的字符串需要拼接,可以依次添加到已拼接的字符串的后面即可。
import requests from bs4 import BeautifulSoup url='https://top.baidu.com/board?tab=realtime' headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37'} res = requests.get(url=url, headers=headers) res.encoding ='utf-8' soup = BeautifulSoup(res.text, 'html.parser') hot_list = soup.find_all(class_='c-single-text-ellipsis') print(hot_list) for hot in hot_list: print(hot.get_text().strip())在这段代码的基础上,利用open()函数将爬取内容写入本地 /home/qingjiao/records.txt 文件
好的,你可以在代码最后加上以下内容来将爬取内容写入指定的文件:
```
with open('/home/qingjiao/records.txt', 'w', encoding='utf-8') as f:
for hot in hot_list:
f.write(hot.get_text().strip() + '\n')
```
这段代码会将爬取到的热搜内容写入指定路径的"records.txt"文件中,每个热搜占一行。请注意,你需要将路径修改为你自己的文件路径。