String Manipulation & Typecasting (1)

1. 文本复制以及连接

1.1 Multiply sign

使用 multiply sigh/乘号* 来复制文本片段。

乘号复制文本举例:
print("Hi" * 3) # output: HiHiHi
print("*" * 10)# output:**********

1.2 连接

1.2.1 使用 plus sign 加号连接文本

加号连接文本举例:text1 = "I am"
text2 = "here"
print(text1+ "空格" + text2)#output I am here

1.2.2使用 Comma sign 逗号连接文本

逗号连接文本举例:text1 = "I am"
text2 = "here"
print(text1, text2)#output I am here

使用逗号和加号连接文本的区别在于,逗号连接文本在 python 中会自动加上空格

2.连接 string 字符串

2.1 使用 f-string 连接字符串

要打印字符串,你也可以使用Python的字符串格式,或者叫做 f-string。

英文:To print a string, you can also use Python's string formatting, or what is called f-strings.

f-string 输出字符串举例:
print(f'Hello World') #output: Hello World

2.1.1使用 f-string 连接变量

如果希望在字符串文本中包含打印变量,只需将变量括在左大括号和右大括号中

英文:If you want to include printing a variable inside your string literal, you just need to enclose your variable within an open curly bracket, and a close curly bracket.

f-string 输出变量举例:print(f'Hello World')
text1 = "I am"
text2 = "here"
print(f'Hi, {text1} {text2}') # output : Hi, I am here

注意: f-string 中,print 函数中的空格会被实际输出
使用 f-string, 能够让你的代码优雅且可读

3. 通过索引位置提取文本

通过引用文本从 LEFT 到 RIGHT 的索引位置,我们可以提取文本的任何部分

英文:We can extract any portion of a text, by >referencing to its index position from LEFT to >RIGHT.

如下我们将使用的例子:

hello jack 包含空格共 10 个字符位

3.1 正序索引调用

使用左方括号和右方括号将索引括起来。 - 索引总是用一个左方括号和一个右方括号括起来。 - 从左侧开始的第一个索引位置总是 index 0 而不是 1。

英文:Use the open and close square brackets to enclose the index. Indexes are always enclosed by an open and a close square bracket. The first index position from the LEFT is always index 0 and NOT 1.

例子:
text = "Hello Jack"
print(text[0])# output: H  ## 因为大写 H 是字符串"Hello Jack" 从左数第一个字符print(text[4])# output: o  ## 同理小写 o 是字符串"Hello Jack" 从左数第4个字符

3.2 逆序索引调用

我们还可以通过"从右到左"引用文本的索引位置, 来提取文本的任何部分。 * 右边第一个位置是index -1。

英文:We can also extract any portion of a text, by referencing its index position from RIGHT to left instead. * The first position from the RIGHT is index -1.

例子:
text = "Hello Jack"
print(text[-1])# output : k## “k”是字符串 Hello Jack 从右开始索引的第一个字符    print(text[-9])# output : e## “e”是字符串 Hello Jack 从右开始索引的第 9 个字符

总结:

因此:正的索引位置从左开始,并从0、1、2、3、4、5 等开始, 而负的索引位置从右开始,并从-1、-2、-3、-4、-5、-6等开始.

英文: So, positive index positions start from the left, and start with 0, 1, 2, 3, 4, 5 and so on
while negative index positions start from the right, and start with -1, -2, -3 -4 -5, -6 and so on

3.3 提取部分字符串

For the same string, you can either use the positive indexes or negative indexes.

3.3.1 如何调取前五个字符

只需表述为”:5“即可

To extract the first 5 characters, put a colon BEFORE the index.

例子:
text = "Hello Jack"
print(text[:5])# output: Hello##

3.3.2 如何提取后 3 个字符:

只需表述为:”-3:“

To extract the last 3 characters, put a colon AFTER the index, and use NEGATIVE

例子:
text = "Hello Jack"
print(text[-3:])# output: ack##

3.3.3 如何提取字符串中的片段

我们还可以提取字符串文本的片段,方法是先放入一个起始索引,后跟冒号,然后放入一个结束索引。例如:print(text[6:10])

We can also slice a string literal, by putting a >starting index, followed by the colon, and then >putting an ending index.

  • 注意,输出包含起始索引,不包含结尾索引。 英文:Note that the starting index is inclusive, while the ending index is exclusive.
正向提取例子:
text = "Hello Jack"
print(text[6:10])# output: Jack## 输出文本6:10,将输出Jack,因为起始索引位置6是J字符,结束索引位置 10-1 是k字符。

如上的例子 10-1 索引, 如果片段的结束索引数字是10,那么意味 着片段中包含的最后一个索引位置数不是10,而是10-1,因为最后 一位索引位置是不包含在内的。
因此,请注意,在确定片段中包含的最后一个字符时,它始终是结束索引位置并减 1.

例如:
text1 = "Hello"
print(text[2:4])# output: ll## 实际输出的是 第 2 到 3 位;即 4-1
再举例一个长的 12 位的字符串
text1 = "HelloTheWorld" 共 12 位置, 从 0 - 11 位
print(text[8:12])# output: Worl## 实际输出的是 第 8 到 11 位;即 12-1

3.3.4 逆向提取片段

如果我们想通过从右向左索引来逆向提取片段,我们可以对索引使用负号,note! 同样,起始索引是包含的,而结束索引是不包含在内的。

Now, if we want to slice by counting from RIGHT to left instead, we can use the NEGATIVE sign for the index. Note that again the starting index is inclusive, while the ending index is exclusive.

逆向索引提取例子:
text = "Hello Jack"
print(text[-6:-9])# output: ell## 如上图所示:输出的是 -9 到 -7 的内容, 即 -9对应字母 e, -6减去 1 等于 -7 对应字母 l.

3.3.5 区域提取

我们还可以通过只放一个起始索引,后跟冒号,而不放结束索引来分割字符串文字

We can also slice a string literal, by putting only a starting index, followed by the colon, and NOT putting an ending index

``` 正向区域索引提取例子: text = "Hello Jack" print(text[4:])

# output: o Jack
## 如上图所示: 从左往右索引,因为起始索引位置4是 o 字符,而且由于我们不放结束索引,所以它将包含字符串文字的最后一个字符,即 k 字符。
![](media/15783570801942/15816953873062.jpg)

逆向区域索引提取例子: text = "Hello Jack" print(text[-4:])

# output: Jack
## 如上图所示,因为起始索引位置 -4 是 J 字符,而且由于们不放结束索引,所以它将包含直到字符串的最后一个字符,即 k 字符。

```

完毕, 待总结。

词汇:

  1. multiply sign 乘号
  2. concatenate text to join them together 翻译:将文本连接在一起
  3. open and close curly brackets 大括号
  4. square bracket 方括号

发布时间: 2020 年 2 月 15日

知乎链接: 字符串处理以及类型转换 1

当前可任意转载,转载请保存以上信息即可。无需获得授权.

fastjson转换时有大括号或者冒号或者有中括号_[Python Basic] 字符串处理以及类型转换 1...相关推荐

  1. fastjson转换包含date类型属性的对象时报错com.alibaba.fastjson.JSONException: For input string 解决方法

    fastjson转换包含date类型属性的对象时报错com.alibaba.fastjson.JSONException: For input string 解决方法 pojo里面的date属性上要加 ...

  2. fastjson转换json字符串key的首字母小写变大写的解决办法

    问题描述 在开发过程中,由于接口文档的描述,要求json字符串的key首字母为大写,而java 的规范要求在定义bean的时候,成员属性是首字母小写的驼峰格式,java的基本规范一定要遵守,否则会出现 ...

  3. python f{} 字符串用法详解(含冒号用法)

    ... 自定义格式:对齐.宽度.符号.补零.精度.进制等 f-string采用 {content:format} 设置字符串格式,其中 content 是替换并填入字符串的内容,可以是变量.表达式或函 ...

  4. python如何小写p转换p_Python进阶---python 中字符串大小写转换

    python中字符串的大小写转换和判断字符串大小写的函数小结: 一.pyhton字符串的大小写转换, 常用的有以下几种方法: 1.对字符串中所有字符(仅对字母有效)的大小写转换,有两个方法: prin ...

  5. Python一些字符串判断和转换

    设s是字符串: s.isalnum()      判断所有字符都是数字或者字母 s.isalpha()  判断所有字符都是字母 s.isdigit()  判断所有字符都是数字 s.islower() ...

  6. python把字符串转化为字典_python 将字符串转换成字典dict的各种方式总结

    1)利用eval可以将字典格式的字符串与字典户转 >>>mstr = '{"name":"yct","age":10}' ...

  7. python中字符串大小写转换常用操作

    python中字符串的大小写转换操作 方法名 操作说明 upper() 把字符串中的字符全部转换为大写 lower() 把字符串中的字符全部转换为小写 swapcase() 把原字符串中的字符,大写变 ...

  8. python 中字符串大小写转换

    python中字符串的大小写转换和判断字符串大小写的函数小结: 一.pyhton字符串的大小写转换, 常用的有以下几种方法: 1.对字符串中所有字符(仅对字母有效)的大小写转换,有两个方法: prin ...

  9. python中字符串的使用04字符串大小写转换、删除空白字符

    python中字符串的使用04字符串大小写转换.删除空白字符 1.字符串的大小写转换 字符串的大小写转换就是将大写字母改为小写字母或者将小写字母改为大写字母.python中字符串大小写转换的方法有:用 ...

最新文章

  1. 基于梯度方向、极化变换和聚类算法的图像主特征直线检测
  2. python爬图片教程_python爬去妹子网整个图片资源教程(最详细版)
  3. 优化问题都需要哪些数学基础?
  4. centos在线安装svn
  5. css 解决fixed 布局下不能滚动的问题
  6. eclipseweb开发遇到的bug
  7. $.cookie is not a function
  8. Spring技术栈系列教程-- SpringMVC 、SpringBoot 、SpringCloud
  9. pandas使用dataframe读写mysql数据库
  10. 人脸方向学习(四):Face Recognition-SphereFace解读
  11. 基于51单片机的数字电流电压表
  12. 猜单词游戏。计算机随机产生一个单词,打乱字母顺序,供玩家去猜 a.准备一组单词,随机抽取一个b.将抽取的单词作为答案,打乱字母顺序,显示给玩家,供其猜测c.猜测错误继续猜测或以空字符串.
  13. linux下打印pdf文件很慢,打印机打印pdf文件特别慢怎么解决
  14. Swift语言难度大吗?适不适合零基础学习?
  15. 【Java入门基础第10天】Java常用的转义字符
  16. OSX: 命令行制作U盘Recovery HD
  17. 2021年第16届首尔国际电视节圆满落幕
  18. IDEA中MAVEN项目如何打包JAR包
  19. python多因子量化选股模型_【邢不行|量化小讲堂系列12-Python量化入门】法码三因子选股模型,有多少人可以跑赢...
  20. 研究OCL功率放大电路的输出功率和效率

热门文章

  1. jQuery自适应倒计时插件
  2. css的再深入9(更新中···)
  3. 浅谈一下我了解的PWA
  4. css 浮动和清除浮动
  5. break、continue、return的区别
  6. ASP.NET获取客户端、服务器端基础信息
  7. angular js实现开关效果
  8. mysql 5.1由于Host为localhost的用户为空,密码为空,导致本地用户无法登陆。
  9. 实验:sigsuspend(),sigprocmask()
  10. Fedora 20 配置