码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • String formatting


    BIT502 Fundamentals of Programming : Contents : Topic 2: Programming concepts : Operations and calculations : String formatting

    two solutions to make string formatting easy and to avoid the above situation.

    • a formatted string 

      The formatted string has the following syntax: f: "some_string_with_variable_insertion"  Wherever we are required to insert a variable, we use {name_of_variable}. 

    • using a placeholder.(use the operator ‘%’ as a placeholder within a string)
    1. age = 23
    2. weight = 84
    3. height = 1.89
    4. name = "John Smith"
    5. formatted_text = f"Hi. My name is {name}. I am {str(age)} years old. My weight is {str(weight)}Kg and height is {str(height)}m"
    6. print(formatted_text)

    Output:

    Hi. My name is John Smith. I am 23 years old. My weight is 84Kg and height is 1.89m

    1. name = "Marsha"
    2. age = 47
    3. height = 1.63
    4. message1 = "My name is %s." % name # %s is a string placeholder inside the string. Note that the % outside the string
    5. #indicates which data to be replaced against the placeholder. The data type should
    6. #match the placeholder (in this case %s is for string and the variable name is string)
    7. message2 = "I am %d years old" % age # %d is for integer
    8. message3 = "My height is %g m." % height # %f is for float
    9. #For multiple insertion
    10. big_message = "My name is %s. I am %d years old and my height is %g metres." % (name,age,height)
    11. print(message1)
    12. print(message2)
    13. print(message3)
    14. print(big_message)

    Output

    My name is Marsha.

    I am 47 years old

    My height is 1.63 m.

    My name is Marsha. I am 47 years old and my height is 1.63 metres.

    1. name = "Sarah"
    2. number_courses = 3
    3. easy_course = "BIT502"
    4. message1 = "%s is enrolled in %d courses." % (name,number_courses)
    5. message2 = f"Hi there! {message1} The easiest course is {easy_course}"
    6. print(message2)

    Output

    Hi there! Sarah is enrolled in 3 courses. The easiest course is BIT502

  • 相关阅读:
    web前端期末大作业(我的家乡广安网页设计与制作)HTML+CSS网页设计实例 企业网站制作
    JAVA泛型
    react事件系统(新老版本)
    【第2章 Node.js基础】2.1 JavaScript基本语法
    论文阅读 - Learning Human Interactions with the Influence Model
    POI及EasyExcel【Java提高】
    jQuery学习:onload和read区别(时间早晚 监听个数)
    rk dp 原理分析和调试
    HCIA VLAN间通信 多臂路由与单臂路由
    blender assetBrowser 资产浏览器
  • 原文地址:https://blog.csdn.net/hnanxihotmail/article/details/127598277
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号