python字符串操作练习题
时间: 2023-08-08 16:10:53 浏览: 105
好的,下面是一些 Python 字符串操作练习题:
1. 编写一个程序,要求用户输入一个字符串,然后将该字符串反转并输出。
2. 编写一个程序,要求用户输入一个字符串,然后将其中的所有大写字母转换为小写字母,并输出结果。
3. 编写一个程序,要求用户输入一个字符串,然后将其中的所有小写字母转换为大写字母,并输出结果。
4. 编写一个程序,要求用户输入一个字符串和一个子字符串,然后输出该子字符串在原字符串中出现的次数。
5. 编写一个程序,要求用户输入一个字符串和一个正整数 n,然后输出该字符串中长度为 n 的所有子串。
6. 编写一个程序,要求用户输入一个字符串和一个字符,然后输出该字符在字符串中出现的位置。
7. 编写一个程序,要求用户输入一个字符串,然后将其中所有的空格删除,并输出结果。
8. 编写一个程序,要求用户输入一个字符串,然后输出其中所有数字的和。
9. 编写一个程序,要求用户输入一个字符串,然后输出其中所有单词的首字母大写的结果。
10. 编写一个程序,要求用户输入一个字符串,然后输出其中所有单词的首字母大写并且单词倒序的结果。
希望这些练习题可以帮助你提高 Python 字符串操作的能力。
相关问题
Python字符串操作练习题
好的,以下是一些Python字符串操作的练习题:
1. 编写一个程序,输入一个字符串,判断该字符串是否为回文串。
2. 编写一个程序,输入一个字符串,统计其中每个字符出现的次数,并将结果输出。
3. 编写一个程序,输入一个字符串,将其中所有的单词首字母大写,其余字母小写,然后输出结果。
4. 编写一个程序,输入一个字符串和一个子串,在该字符串中查找该子串出现的次数,并输出结果。
5. 编写一个程序,输入一个字符串,将其中所有的数字字符替换为英文单词(例如将"123"替换为"one two three"),然后输出结果。
希望这些练习题能够帮助你熟练掌握Python字符串操作的技能。
python字符串操作练习
Python字符串操作练习题可以包括判断回文字符串、反转字符串、字符串变形(字母大小写切换)以及找到最长回文子串等任务。下面是几个例子:
1. 判断回文字符串(双指针):
```python
class Solution:
def judge(self, s: str) -> bool:
left = 0
right = len(s) - 1
while left < right:
if s[left != s[right]:
return False
left += 1
right -= 1
return True
```
2. 反转字符串:
```python
class Solution:
def reverse(self, s: str) -> str:
return s[::-1]
```
3. 字符串变形(字母大小写切换):
```python
class Solution:
def transform(self, s: str) -> str:
transformed = ""
for char in s:
if char.islower():
transformed += char.upper()
elif char.isupper():
transformed += char.lower()
else:
transformed += char
return transformed
```
4. 找到最长回文子串:
```python
class Solution:
def longestPalindrome(self, s: str) -> str:
longest = ""
for i in range(len(s)):
for j in range(len(s), i, -1):
substring = s[i:j]
if substring == substring[::-1 and len(substring) > len(longest):
longest = substring
return longest
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)