#使用{}的方法
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 添加字符串的七种方法的更多相关文章

  1. Python拼接字符串的七种方式

    忘了在哪看到一位编程大牛调侃,他说程序员每天就做两件事,其中之一就是处理字符串.相信不少同学会有同感. 几乎任何一种编程语言,都把字符串列为最基础和不可或缺的数据类型.而拼接字符串是必备的一种技能.今 ...

  2. python连接字符串的几种方法--转子(香草拿铁的园子)

    一. str1+str2 string类型 ‘+’号连接 >>> str1="Good" >>> str2="Luck" & ...

  3. Python拼接字符串的7种方法

    1.直接通过+操作: s = 'Python'+','+'你好'+'!'print(s) 打印结果: Python,你好! 2.通过join()方法拼接: 将列表转换成字符串 strlist=['Py ...

  4. python 格式化字符串的三种方法

    1)%格式化方法 >>> a = "this is %s %s" % ("my", "apple") >>&g ...

  5. linux shell脚本编程笔记(四): 获取字符串长度的七种方法

    获取字符串长度的七种方法 1. \${#str} 2.awk的length 备注:1) 最好用{}来放置变量2) 也可以用length($0)来统计文件中每行的长度 3.awk的NF 备注: -F为分 ...

  6. (转)Shell中获取字符串长度的七种方法

    Shell中获取字符串长度的七种方法 原文:http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell脚本中很常用,下面 ...

  7. python—字符串拼接三种方法

    python—字符串拼接三种方法   1.使用加号(+)号进行拼接 字符串拼接直接进行相加就可以,比较容易理解,但是一定要记得,变量直接相加,不是变量就要用引号引起来,不然会出错,另外数字是要转换为字 ...

  8. Python 3 格式化字符串的几种方法!

    Python 3 格式化字符串的几种方法! %s和%d,%s是用来给字符串占位置,%d是给数字占位置,简单解释下: a = 'this is %s %s' % ('an','apple') 程序输出的 ...

  9. Python下载网页的几种方法

    get和post方式总结 get方式:以URL字串本身传递数据参数,在服务器端可以从'QUERY_STRING'这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据(只能是字符串,比如在 ...

随机推荐

  1. JQuery select,checkbox用法 文本框只能输入数字

    记录一下,方便查找 a.文本框只能输入数字 onkeyup='this.value=this.value.replace(/\D/gi,"")' eg: <input typ ...

  2. 吴裕雄--天生自然 PHP开发学习:表单 - 验证邮件和URL

    $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $ ...

  3. c语言中常用的串运算

    调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char from) 串连接,strca ...

  4. ZJNU 1217 - 航线问题——高级

    将所有航线的其中一边排序后,另一边进行类dp 定义一个数组c,c[i]表示在所有能够开通i条航线的组合中,位置序号最大的那条航线的序号的最小值 比如下面一个样例 1 3 2 4 3 1 4 2 此时对 ...

  5. 论文翻译——Deep contextualized word representations

    Abstract We introduce a new type of deep contextualized word representation that models both (1) com ...

  6. 吴裕雄--天生自然TensorFlow高层封装:Keras-返回值

    # 1. 数据预处理. import keras from keras.models import Model from keras.datasets import mnist from keras. ...

  7. 吴裕雄--天生自然 JAVA开发学习:多态

    Parent p = new Child(); public class Test { public static void main(String[] args) { show(new Cat()) ...

  8. Git-GitHub-GitLab三者之间的联系及区别

    在讲区别以及联系之前先简要的介绍一下,这三者都是什么(本篇文章适合刚入门的新手,大佬请出门左转) 1.什么是 Git? Git 是一个版本控制系统. 版本控制是一种用于记录一个或多个文件内容变化,方便 ...

  9. 字典|字典和数组嵌套|keys()|value()|item()|del

    python 中没有i++! dic = { 'a':5, 'kelv':'ooo', 5:2333, 2333:'yoo' } print dic[dic[dic['a']]] print dic ...

  10. CF1137C Museums Tour(tarjan+DP)

    由于d很小,所以可以把每个点拆成d个点,然后对于边(x,y),连边时连接((x,i),(y,i+1))及((x,d),(y,1)).然后可以对这样连的边跑一遍tarjan缩点.然后直接暴力DP即可.不 ...