python获得用户输入的一个字符串(长度3)_python3 字符串属性(一)
5、字符串编解码
{
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode
作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编(encode)
成另一种编码。但是,Python 2.x的默认编码格式是ASCII,就是说,在没有指定Python源码编码
格式的情况下,源码中的所有字符都会被默认为ASCII码。也因为这个根本原因,在Python 2.x中
经常会遇到UnicodeDecodeError或者UnicodeEncodeError的异常。
原则
decode early, unicode everywhere, encode late,即:在输入或者声明字符串的时候,
尽早地使用decode方法将字符串转化成unicode编码格式;然后在程序内使用字符串的时候统一使用
unicode格式进行处理,比如字符串拼接、字符串替换、获取字符串的长度等操作;最后,在输出字符
串的时候(控制台/网页/文件),通过encode方法将字符串转化为你所想要的编码格式,比如utf-8等。
https://segmentfault.com/a/1190000002966978
}
S.encode(encoding='utf-8', errors='strict') -> bytes 以encoding指定的编码格式对字符串进行编码,输出的字节不是字符串,类型不同,属性不同。
6. S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
1 >>> a='hello world!'
2 >>> a.endswith('!')3 True4 >>> a.endswith('o',0,5)5 True6 >>> a.startswith('h')7 True8 >>> a.startswith('w',6)9 True10 >>> a.startswith('hello')11 True
7、 S.expandtabs(tabsize=8) -> str 把字符串的tab字符(\t)转化为空格,如不指定tabsize,默认为8个空格
1 >>> a='hello world'
2 >>>a.expandtabs()3 'hello world'
4 >>> a='\t hello world \t'
5 >>>a.expandtabs()6 'hello world'
7 >>> a.expandtabs(tabsize=2)8 'hello world'
8、S.find(sub[, start[, end]]) -> int 检测sub是否在字符串中,如果在则返回index,否则返回-1,start,end为可选参数,决定范围。(返回左侧第一个)
S.rfind(sub[, start[, end]]) -> int (返回右侧第一个)
Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
1 >>> a='hello world'
2 >>> a.find('h')3 0
4 >>> a.find('h',1,3)5 -1
6 >>> a.find('o',1,4)7 -1
8 >>> a.find('o',1,5)9 4
9、
S.index(sub[, start[, end]]) -> int 没有找到返回ValuueError错误 (返回左侧第一个)
Like S.find() but raise ValueError when the substring is not found. 没有找到返回 -1
S.rindex(sub[, start[, end]]) -> int (返回右侧第一个)
Like S.rfind() but raise ValueError when the substring is not found.
1 >>>a2 'hello world'
3 >>> a.index('ll')4 2
5 >>> a.index('lll')6 Traceback (most recent call last):7 File "", line 1, in
8 ValueError: substring not found
10、S.isalnum() -> bool Return True if all characters in S are alphanumeric
都是字母和数字字符返回True,否则返回False
>>> a.isalnum()
False
>>> a='123#$":,./'
>>> a.isalnum()
False
>>> a='123'
>>> a.isalnum()
True
>>> a='123a'
>>> a.isalnum()
True
>>> a='123a('
>>> a.isalnum()
False
11、S.isalpha() -> bool Return True if all characters in S are alphabetic
判断字符串是否为字母
>>> a='123'
>>> b='123a'
>>> c='abc'
>>> a.isalpha()
False
>>> b.isalpha()
False
>>> c.isalpha()
True
python获得用户输入的一个字符串(长度3)_python3 字符串属性(一)相关推荐
- python获得用户输入的一个字符串(长度3)_Python笔记(3)-字符串
字符串表示方式 第一种方式: str1 = 'redhat' 第二种方式: str2 = "redhat" 第三种方式: str3 = """redh ...
- python123子字符串输出_获得用户输入的一个字符串,替换其中出现的字符串py为python”,输出替换后的字符串。...
[填空题]获得用户输入的一个字符串,输出其中字母'a'的出现次数.s=input()print(s._______('a')) [单选题]给出如下代码TempStr ="Hello Worl ...
- Python根据用户输入的2进制字符串转8进制
Python根据用户输入的2进制字符串转8进制 问题: 从键盘输入一个由 1 和 0 组成的二进制字符串 s,转换为八进制数输出显示在屏幕上.例如输入'1100',输出'14' 实现1: # 请输入一 ...
- python计算n的32次方_获得用户输入的一个整数N,计算并输出N的32次方。_学小易找答案...
[多选题]材料的力学性质 [单选题]已知在计算机中存储了"大学计算机基础"这样一串汉字,它们所占用的存储空间为( )二进制位. [单选题]以下不是python的注释方式是( ) [ ...
- Python中生成一个指定长度的随机字符串实现示例
方法一: 定义一个函数,参数为所要生成随机字符串的长度.通过random.randint(a, b)方法得到随机数字,具体函数如下: 1 2 3 4 5 6 7 8 9 10 def generate ...
- Python中用户输入与while循环
文章目录 前言 一.用户输入 1.输入字符串 2.使用函数int()获取数值输入 3.求模运算符的应用 二.while循环 1.简单的while循环 2.通过用户输入退出while循环 3.使用标志 ...
- 重温Python基础——用户输入和while循环
人生苦短,我用Python 序言 函数input()的工作原理 使用int()来获取数值输入 while循环简介 使用break退出循环 在循环中使用continue 避免无限循环 删除为特定值的所有 ...
- python等待用户输入_Python等待时间,等待用户输入
python等待用户输入 Sometimes we want our python program to wait for a specific time before executing the n ...
- python获得用户输入的一段文字将这段文字进行垂直输出_Python练习题2020
习题2 #获得用户输入的一个整数N,计算并输出N的32次方 a=eval(input("请输入一个整数:")) v = a**3 print("N的32次方为{}&quo ...
最新文章
- Python实战案例,CV2模块,Python实现抖音字符视频
- Spark 应用程序调优
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects...
- linux下JNI的demo
- 树莓派3 kali linux很卡,树莓派3装kali Linux 成功写入 但是点不亮 为什么?
- docker下用keepalived+Haproxy实现高可用负载均衡集群
- Zookeeper UI管理界面安装
- 谭浩强c++程序设计知识点思维导图
- HTTP代理实现请求报文的拦截与篡改2--功能介绍+源码下载
- Spring : @EnableAutoConfiguration注解
- jQuery学习教程 基础篇 归档
- # JDK7+ MethodHandle
- im即时通讯源码带教程/uniapp即时通讯源码,附安装教程
- 2022-2028年中国手机银行行业市场竞争态势及未来前景分析报告
- 全国大江大河实时水情数据下载
- 微信小程序使用echarts
- 基于SpringBoot实现邮箱找回密码
- Cocos Creator之打包设置横竖屏
- grant User 使用
- 华为联运服务登录支付签名