惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
S
SegmentFault 最新的问题
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
U
Unit 42
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - Franky
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - xiaobin80

Hello MFC - Add Resource and About Dialog Hello MFC - The Message Map my first mfc application Development Environment Preparation Clang IDE的选择在Windows Visual Studio 2017 and Fx EOL 跟我学python(4)- 正则 跟我学python(3)--- 带shell功能的hello world 跟我学python(2)- 第一个程序 跟我学python --- 搭建开发平台 Debian server 安装 debian 与 ubuntu 之 - sudo Try Value 计算文件CRC32数值 docker ce on Debian 使用hugo在gitee上写blog spring cloud gateway - RequestRateLimiter postman使用 改进的冒泡算法 Create User - mysql COM调用 – VB、PB J-Link clone问题 修复山寨版的J-Link
跟我学python(5)- 匹配数字
xiaobin80 · 2026-02-03 · via 博客园 - xiaobin80

读取输入的字符串,是否是数字;

转换为浮点数并输出。

raw_input

input([prompt])

re.match

re.match(pattern, string, flags=0)

类型转换

  • (1)字符串转浮点
float(x) 
  • (2)数字转字符串
str(x)

代码

#coding=utf-8
'''
Created on 2019年1月3日

@author: xiaobin
'''
import re
'''
#mre22_1.pl
#! /usr/bin/perl -w

# Mastering Regular Expressiona: Chapter 2 Section 2.
# first program

print "Enter a temperature in Celsius:\n";
$celsius = <STDIN>;
chomp($celsius);

if ( $celsius =~ /^[0-9]+$/) {
    $fahrenheit = ($celsius * 9 / 5) + 32;
    print "$celsius C is $fahrenheit F\n";
}
else {
    print "Expecting a number, so I don't understand \"$celsius\".\n";
}
'''
# str1 = raw_input("Enter a temperature in Celsius: ") # python 2
str1 = input("Enter a temperature in Celsius: ")

res = re.match('^[0-9]+$', str1)

if res :
  celsius = float(str1)
  fahrenheit = (celsius * 9 / 5) + 32
  print(str1 + "C is " + str(fahrenheit) + "F")
else :
    print("Expecting a number, so I don't understand " + '\"' + str1 + '\"')

output:

Enter a temperature in Celsius: 38
38C is 100.4F

Reference:

  1. python files io
  2. search vs match
  3. python numbers