python not in string_python学习笔记7-- string类型

1. math模块

math模块在很多语言中都有,python也不例外,它主要提供数学计算的许多公式,包括三角函数、指数对数、角度转换等,其中还定义了两个常量圆周率pi和自然底数e。可以用import math或from math import * 来导入math模块,区别是前一种导入要在调用函数前加上math. ;后一种直接使用函数即可。

math中的许多函数都比较常见,要用的时候查阅帮助文档就可以。记下几个今天学到的函数:

degree(x):将弧度x转换为角度;

radians(x):将角度x转换为弧度;

modf(x):将x转换为(f,i)表示,其中f表示x的小数部分,i表示x的整数部分,f和i都是浮点型的。

2. string

python中的string可以直接输出,且有两种表示方法,单引号表示 ('str')、双引号表示("str")。通常情况下,如果要输出的string中包含单引号,则string用双引号表示;要输出的string中包含双引号,则string用双引号表示。如果要输出的string 中既包含单引号,又包含双引号,则需要用转义字符('\')。比较常见的如果string为多个字符,用双引号;单个字符用单引号。

#representation

print (' "Welcome to python", he said ')

print (" 'Welcome to python', he said ")

#blackslash \

print ('"welcome to python", he said. \'yes\', I\'m glad ')

1)创建strings

可以用str()函数创建,如s1=str("welcome"),也可以直接创建,如s2="welcome",两者等同。另外,还可以用str()函数来将数字转换为string,如a = str(5);则a的结果为'5'。

#str() function

s = str(5.0)    # s : '5.0'

print (s)       # 5.0

2)len,max,min

len()函数返回字符串中包含的字符个数;max()返回字符串中ASCII码值最大的字符;min()返回字符串中ASCII码值最小的字符。如:

s = "welcome"

print (len(s))   # 7

print (max(s))   # w

print (min(s))   # c

3)两个字符串的联接

a. 若两个string靠在一起时,没有其他操作符号,则默认联接这两个字符串;

b. 显式的 “+”操作符,用来联接两字符串;

#concatenation

s = "abc" "cde" "feg"

print (s)               # abccdefeg

s = "abc" + "cde" + "feg"

print (s)               # abccdefeg

4)字符串Repetition操作符*

可以用*来重复表示某字符串多次,如s1="welcome",s1*3结果为"welcomewelcomewelcome"。

5)python中的string类型有下标:从0到n-1。我们可以利用下标来读取相应的内容

#read by sub

s1 = "hello World!"

print (s1[1])          # e

print (s1[0:2])        # he

print (s1[:8])         # hello Wo

print (s1[2:])         # llo World!

print (s1[-100:2])     # he

print (s1[-100:100])   # hello World!

print (s1[-8:-2])      # o Worl

print (s1[-1])         # !

注意的是,python中的string有负数下标,范围为-1到-n(实际上可以看成是[-1+len(s)]),-1是最后一个字符,-n是第一个。另外,[x:y]表示的是从第x位到第y-1位之间的内容。如果x小于最小下标-n,则它默认就从第一个字符开始,如果y大于字符串最大下标n-1,则它默认到最后一个字符。

6)python中的string一旦赋值,不能更改其内容;

#string is immutable

str = "I'm immutable."

str[5] = 'f'

print (str)如果执行以上操作,python会报出'str' object does not support item assignment 的错误

7)in,not in

可以用in和not in来判断子字符串是否在某字符串中,返回值是bool型变量True和False;如

s1 = "what a nice day!"

print ("hat" in s1)  # True

print ("bat" in s1)  # False

8)字符串比较

可以通过比较操作符(==,!=,>,>=,

print ("black"=="black")  # True

print ("black"!="white")  # True

print ("black">"white")   # False

print ("black"<="white")  # True

9)字符串的遍历

有两种字符串遍历方式:

不带下标的遍历方式: for ch in s: print(ch)

带下标的遍历方式:for i in range(0,len(s),1): print(s[i])

10)isalnum()等内置函数

isalnum():如果字符串是由字母和数字组成的,则返回True,否则返回False;

isalpha():如果字符串是有字母组成的,则返回True,否则返回False;

isdigit():如果字符串全由数字组成的,则返回True,否则返回False;

isidentifier():如果字符串是python的关键字,则返回True,否则返回False;

islower():如果字符串全是小写,返回True,否则返回False;

isupper():如果字符串全是大写,则返回True,否则返回False;

isspace():如果字符串全由whitespace字符组成,则返回True,否则返回False;

举个例子:

print ("welcome123".isalnum())  # True

print ("welcome&".isalpha())    # False

print ("wel to py".isalnum())   # False

print ("wel to py".isalpha())   # False

print ("20140722".isdigit())    # True

print ("print".isidentifier())  # True

print ("WELCOME".islower())     # False

print ("WELCOME".isupper())     # True

print ("\t\n".isspace())        # True

11)查找子串函数

endswith(s1):如果字符串以s1结尾,则返回True,否则返回False;

startwith(s1):如果字符串以s1开始,则返回True,否则返回False;

find(s1):如果字符串中包含s1,则返回字符串中最先出现的s1的起始下标,否则不包含,返回-1;

rfind(s1):如果字符串中包含s1,则返回字符串中最后出现的s1的起始下标,否则不包含,返回-1;

count(substring):返回字符串中包含substring的个数;

举个例子:

s = "welcome to python"

print (s.endswith("hon"))    # True

print (s.startswith("wel"))  # True

print (s.find("you"))        # -1

print (s.rfind("o"))         # 15

print (s.count("o"))         # 3

12)字符串转换内置函数

capitalize():返回一个字符串,只有首字符大写;

lower():返回一个字符串,所有字符小写;

upper():返回一个字符串,所有字符大写;

title():返回一个字符串,每个单词的首字符大写;

swapcase():返回一个字符串,大写变小写,小写变大写;

replace(old,new):返回一个字符串,所有old全部转换为new表示;

举个例子:

s = "Welcome to Python, you Will enJoy IT!"

print (s.capitalize())  # Welcome to python, you will enjoy it!

print (s.lower())       # welcome to python, you will enjoy it!

print (s.upper())       # WELCOME TO PYTHON, YOU WILL ENJOY IT!

print (s.title())       # Welcome To Python, You Will Enjoy It!

print (s.swapcase())    # wELCOME TO pYTHON, YOU wILL ENjOY it!

print (s.replace("o","x"))  # Welcxme tx Pythxn, yxu Will enJxy IT!

值得注意的是,因为字符串是不可改变的,所以这些函数操作后的结果都是生成一个新的字符串。

13)strip内置函数

lstrip():返回一个字符串,删除字符串首部的whitespace字符(包括' ', \t, \f, \r, \n);

rstrip():返回一个字符串,删除字符串尾部的whitespace字符;

strip():返回一个字符串,删除字符串首尾的whitespace字符;

14)格式化字符串

center(width):居中对齐,宽度是width,返回一个新字符串;

ljust(width):左对齐,宽度是width,返回一个新字符串;

rjust(width):右对齐,宽度是width,返回一个新字符串;

举个例子:

s = "welcome"

print (s.center(13))   # '   welcome   '

print (s.ljust(13))    # 'welcome      '

print (s.rjust(13))    # '      welcome'

3. ASCII Code 和 Unicode code

ASCII Code 全称是 American Standard Code for Information Interchange,它共表示128个字符(不加扩展ASCII码),python默认的编码方式就是ASCII码,所以程序中有中文时,系统会将其转换成ASCII码表示,很可能会产生乱码或报错。

Unicode code 是建立在全球各种语言基础上的,通常表示为\uxxxx,涵盖了中文表示。python也支持unicode编码,比如可以在程序中显式的用unicode字符。通常会在文件头上加#coding=utf-8 (注:utf-8是unicode的变种)。

python中有两个函数可以转换ascii字符和数字。

ord(ch) :将ascii字符ch转换成对应的数字;

chr(num):将数字转换成对应的ascii字符;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值