首页 > 编程语言 > 详细

Python 常用的方法

时间:2017-02-03 15:36:43      阅读:190      评论:0      收藏:0      [点我收藏+]

 

用户输入

name = input("please input your name:")       

查看变量类型

print type(name)

强制定义变量为整数

age = int(input("pls input your age:"))

去除开头结尾输入的空格

name = input("pls input your name:").strip()

输出

print(name)    

格式化输出

zara = abc
print(you name %s % (zara))

长度

len(name)

isdigit()方法检测字符串是否只由数字组成

#!/usr/bin/python

str = "123456";  # Only digit in this string
print str.isdigit();

str = "this is string example....wow!!!";
print str.isdigit();

输出结果
True
输出结果 False

split()方法通过指定分隔符对字符串进行切片

str.split(str="", num=string.count(str))
str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num -- 分割次数
返回分割后的字符串列表
>>> str = "this is string example....wow!!!" >>> print (str.split()) [this, is, string, example....wow!!!] >>> print (str.split(i,1)) [th, s is string example....wow!!!] >>> print (str.split(w)) [this is string example...., o, !!!]

 

Python 常用的方法

原文:http://www.cnblogs.com/sxlnnnn/p/6362702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!