python 添加字符串的七种方法
#使用{}的方法
s1 = 'Hello {}! My name is {}.'.format('World', 'Python猫')
print(s1) s2 = 'Hello {0} My name is {1}.'.format('world','Python 猫')
print(s2) s3 = 'Hello {name1}! My name is {name2}.'.format(name1='world',name2='Python 猫')
print(s3) #使用 % 的方法
print('%s %s' % ('Hello', 'world')) #使用()类似元组的方法
s_tuple = ('Hello',' ','world')
s_like_tuple = ('Hello' ' ' 'world') print(s_tuple)
print(s_like_tuple)
type(s_like_tuple) #面向对象模板拼接
from string import Template
s = Template('${s1} ${s2}!')
print(s.safe_substitute(s1='Hello',s2='world')) #+号方式
str_1 = 'Hello world! '
str_2 = 'My name is Python猫.'
print(str_1 + str_2)
print(str_1) #join()拼接方式
str_list = ['Hello','wordl']
str_join1 = ''.join(str_list)
str_join2 = '-'.join(str_list)
print(str_join1)
print(str_join2) #f-string
name = 'world'
myname = 'python_cat'
words = f'Hello {name}. My name is {myname}.'
python 添加字符串的七种方法的更多相关文章
- Python拼接字符串的七种方式
忘了在哪看到一位编程大牛调侃,他说程序员每天就做两件事,其中之一就是处理字符串.相信不少同学会有同感. 几乎任何一种编程语言,都把字符串列为最基础和不可或缺的数据类型.而拼接字符串是必备的一种技能.今 ...
- python连接字符串的几种方法--转子(香草拿铁的园子)
一. str1+str2 string类型 ‘+’号连接 >>> str1="Good" >>> str2="Luck" & ...
- Python拼接字符串的7种方法
1.直接通过+操作: s = 'Python'+','+'你好'+'!'print(s) 打印结果: Python,你好! 2.通过join()方法拼接: 将列表转换成字符串 strlist=['Py ...
- python 格式化字符串的三种方法
1)%格式化方法 >>> a = "this is %s %s" % ("my", "apple") >>&g ...
- linux shell脚本编程笔记(四): 获取字符串长度的七种方法
获取字符串长度的七种方法 1. \${#str} 2.awk的length 备注:1) 最好用{}来放置变量2) 也可以用length($0)来统计文件中每行的长度 3.awk的NF 备注: -F为分 ...
- (转)Shell中获取字符串长度的七种方法
Shell中获取字符串长度的七种方法 原文:http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell脚本中很常用,下面 ...
- python—字符串拼接三种方法
python—字符串拼接三种方法 1.使用加号(+)号进行拼接 字符串拼接直接进行相加就可以,比较容易理解,但是一定要记得,变量直接相加,不是变量就要用引号引起来,不然会出错,另外数字是要转换为字 ...
- Python 3 格式化字符串的几种方法!
Python 3 格式化字符串的几种方法! %s和%d,%s是用来给字符串占位置,%d是给数字占位置,简单解释下: a = 'this is %s %s' % ('an','apple') 程序输出的 ...
- Python下载网页的几种方法
get和post方式总结 get方式:以URL字串本身传递数据参数,在服务器端可以从'QUERY_STRING'这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据(只能是字符串,比如在 ...
随机推荐
- c语言中assert的用法
/************************************************************************* > File Name: assert.c ...
- pandas读取和写入excel多个sheet表单
一.读取单个表单 import pandas as pd excel_reader=pd.ExcelFile('文件.xlsx') # 指定文件 sheet_names = excel_reader. ...
- Maven--设置Http代理
<settings> ... <proxies> <proxy> <id>my-proxy</id> <active>true& ...
- Dynamics CRM - 通过 C# Plugin 来 abandon Business Process Flow
需求说明: 当一个 Entity 存在 Business Process Process 时,有时我们需要改变其状态,在之前写的博客有讲了可以通过 JavaScript 来实现,本篇就来讲一下如何通过 ...
- Windows 常用配置 - 启用长路径
Windows 启用长路径支持 打开注册表编辑器:regedit 找到如下路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSyte ...
- 吴裕雄--天生自然 pythonTensorFlow自然语言处理:Attention模型--测试
import sys import codecs import tensorflow as tf # 1.参数设置. # 读取checkpoint的路径.9000表示是训练程序在第9000步保存的ch ...
- win10远程桌面身份验证错误,要求的函数不受支持
出现原因win10最近安装了一个更新补丁导致的,当然可以直接卸载这个补丁,也可以安装操作来就可以.需要修改注册表里边的项目,添加下面的设置. 解决办法1.win+R 输入 regedit, 打开注册表 ...
- spring+mybatis配置多个数据源
http://www.cnblogs.com/lzrabbit/p/3750803.html
- Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_并行运算-2
1 MATLAB并行计算-从个人桌面到远程集群和云(陈伟/魏奋)视频摘录笔记 https://cn.mathworks.com/videos/parallel-computing-with-matla ...
- [USACO09DEC]牛收费路径Cow Toll Paths(floyd、加路径上最大点权值的最短路径)
https://www.luogu.org/problem/P2966 题目描述 Like everyone else, FJ is always thinking up ways to increa ...