Python字符串右移三位
在Python中对字符串右移三位的方法有很多,下面介绍其中两种方法。
方法一:
使用切片进行字符串移位,代码如下:
``` python
a = input("请输入一个字符串:") # 输入字符串
b = a[-3:] + a[:-3] # 切片进行字符串移位
print(b) # 输出移位后的字符串
```
如图所示,将字符串“abcdef”右移三位得到的结果为“defabc”。

方法二:
使用Python标准库collections中的双向队列deque进行字符串移位,代码如下:
```python
from collections import deque
a = input("请输入一个字符串:") # 输入字符串
b = deque(a) # 将字符串转换为双向队列
b.rotate(3) # 右移三位
print("".join(b)) # 输出移位后的字符串
```
如图所示,将字符串“abcdef”右移三位得到的结果为“defabc”。

Python库自带词云图形状
Python中的词云通常需要自定义词云形状,下面介绍一些Python库自带的词云图形状。
在导入词云库时,可以使用from wordcloud import WordCloud, ImageColorGenerator去导入相关库。
词云库自带的一些形状如下:
1. heart.png:心形
```python
from wordcloud import WordCloud, ImageColorGenerator
wc = WordCloud(background_color="white", width=800, height=600, max_words=2000, mask=plt.imread("heart.png"),
random_state=42, font_path='msyh.ttc')
```
效果如图所示:

2. candy.png:糖果
```python
from wordcloud import WordCloud, ImageColorGenerator
wc = WordCloud(background_color="white", width=800, height=600, max_words=2000, mask=plt.imread("candy.png"),
random_state=42, font_path='msyh.ttc')
```
效果如图所示:

3. duck.png:小黄鸭
```python
from wordcloud import WordCloud, ImageColorGenerator
wc = WordCloud(background_color="white", width=800, height=600, max_words=2000, mask=plt.imread("duck.png"),
random_state=42, font_path='msyh.ttc')
```
效果如图所示:

4. fish.png:小鱼
```python
from wordcloud import WordCloud, ImageColorGenerator
wc = WordCloud(background_color="white", width=800, height=600, max_words=2000, mask=plt.imread("fish.png"),
random_state=42, font_path='msyh.ttc')
```
效果如图所示:

5. moon.png:月亮
```python
from wordcloud import WordCloud, ImageColorGenerator
wc = WordCloud(background_color="white", width=800, height=600, max_words=2000, mask=plt.imread("moon.png"),
random_state=42, font_path='msyh.ttc')
```
效果如图所示:
 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/
昨天再好,也走不回去。明天再难,也要抬脚继续。兔年一切好运!