python.提取身份证号码中的出生日期以类似1992年10月12日这样的形式输出 2.判断一个单词是否是回文,即正向和倒序输出一样 3.输入手机号码,输出时将中间4位用星号代替
时间: 2024-06-10 15:07:34 浏览: 11
1. 可以使用Python中的re模块来实现身份证号码的提取和出生日期的格式化输出,具体实现方法可以参考以下代码:
```python
import re
# 定义身份证号码正则表达式
id_pattern = r'\d{17}[\dX]'
# 从输入中匹配身份证号码
id_num = re.search(id_pattern, input('请输入身份证号码:'))
if id_num:
# 提取出生日期信息
birth_date = id_num.group()[6:14]
birth_year = birth_date[0:4]
birth_month = birth_date[4:6]
birth_day = birth_date[6:8]
# 格式化输出
print('{}年{}月{}日'.format(birth_year, birth_month.lstrip('0'), birth_day.lstrip('0')))
else:
print('未检测到身份证号码')
```
2. 实现回文字符串的判断可以使用Python中的切片操作,具体实现方法可以参考以下代码:
```python
word = input('请输入一个单词:')
if word == word[::-1]:
print('是回文字符串')
else:
print('不是回文字符串')
```
3. 字符串中间4位用星号代替可以通过字符串的切片和拼接操作来实现,具体实现方法可以参考以下代码:
```python
phone_num = input('请输入手机号码:')
if len(phone_num) == 11:
hidden_num = phone_num[0:3] + '****' + phone_num[7:]
print('处理后的手机号码:{}'.format(hidden_num))
else:
print('手机号码格式不正确')
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)