python中美元符号用法_美元符号($)在printf格式字符串中做什么?

I am trying to format in java using the printf statement like in this webpage: Click Here. But I just can't figure out what the purpose of the $ sign is. Can someone please explain this to me?

Input:

java 100

cpp 65

python 50

Expected Output: ( there should be a space instead of _ )

================================

java___________100

cpp___________065

python_________050

================================

My code:

public class Solution {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("================================");

for(int i=0;i<3;i++)

{

String s1=sc.next();

int x=sc.nextInt();

System.out.printf(s1 + "%03d", x);

System.out.println();

}

System.out.println("================================");

}

}

解决方案

It is the Argument Index. You can read the docs. I will try to explain the string from the tutorial for you:

String fmt = "%1$4s %2$10s %3$10s%n";

// format

cnsl.printft(fmt, "Items", "Quantity", "Price");

cnsl.printft(fmt, "-----", "-----", "-----");

cnsl.printft(fmt, "Tomato", "1Kg", "15");

cnsl.printft(fmt, "Potato", "5Kg", "50");

cnsl.printft(fmt, "Onion", "2Kg", "30");

cnsl.printft(fmt, "Apple", "4Kg", "80");

In general the format is %[argument_index$][flags][width][.precision]conversion.

In our example, #$ points to the position within our printft() statement. We have 3 strings to be formatted and hence why our format string has 1$, 2$,3$. The number that follows its the width between each argument. This width begins at 0 which means the actual width would be +1. The s is our conversion to string and the %n new line at the end.

Items Quantity Price

----- -------- -----

Tomato 1Kg 15

Potato 5Kg 50

Onion 2Kg 30

Apple 4Kg 80

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 字符串格式化可以使用字符串的 `format()` 方法或 `%` 运算符来完成。以下是使用 `%` 运算符的字符串格式化示例: ### 字符串格式化示例 ```python # 格式化整数 num = 123 print("整数:%d" % num) # 格式化浮点数 fnum = 3.1415926 print("浮点数:%.2f" % fnum) # 格式字符串 name = "John" print("字符串:%s" % name) # 格式化多个值 age = 30 print("姓名:%s,年龄:%d" % (name, age)) ``` 输出: ``` 整数:123 浮点数:3.14 字符串:John 姓名:John,年龄:30 ``` `%` 运算符支持的格式化字符如下: | 格式化字符 | 描述 | | --- | --- | | `%d` | 整数 | | `%f` | 浮点数 | | `%s` | 字符串 | | `%c` | 字符 | | `%o` | 八进制数 | | `%x` | 十六进制数 | | `%e` | 科学计数法 | | `%g` | 根据值的大小决定使用 `%f` 或 `%e` | 除了上述格式化字符,还可以使用如下格式化选项: | 选项 | 描述 | | --- | --- | | `+` | 在正数前面显示加号 | | `-` | 左对齐 | | `0` | 填充0而不是默认的空格 | | `#` | 为八进制数或十六进制数添加前缀 | | `.` | 精度或字符串最大长度 | 示例: ```python # 格式化选项示例 print("整数:%+d" % num) # 整数:+123 print("浮点数:%0.2f" % fnum) # 浮点数:3.14 print("八进制数:%#o" % num) # 八进制数:0o173 print("十六进制数:%#x" % num) # 十六进制数:0x7b print("科学计数法:%e" % fnum) # 科学计数法:3.141593e+00 print("字符串最大长度:%.3s" % name) # 字符串最大长度:Joh ``` 输出: ``` 整数:+123 浮点数:3.14 八进制数:0o173 十六进制数:0x7b 科学计数法:3.141593e+00 字符串最大长度:Joh ``` 除了 `%` 运算符,还可以使用字符串的 `format()` 方法进行字符串格式化。`format()` 方法使用花括号 `{}` 作为占位符,可以使用位置参数或关键字参数指定要格式化的值。 ### 使用 `format()` 方法进行字符串格式化示例 ```python # 使用位置参数进行格式化 print("姓名:{},年龄:{}".format(name, age)) # 使用关键字参数进行格式化 print("姓名:{n},年龄:{a}".format(n=name, a=age)) # 使用数字索引进行格式化 print("姓名:{0},年龄:{1}".format(name, age)) # 使用字典进行格式化 person = {"name": "Mike", "age": 25} print("姓名:{name},年龄:{age}".format(**person)) ``` 输出: ``` 姓名:John,年龄:30 姓名:John,年龄:30 姓名:John,年龄:30 姓名:Mike,年龄:25 ``` `format()` 方法支持的占位符与 `%` 运算符相同,但使用方式不同。示例: ```python # 使用 format() 方法进行格式化 print("整数:{}".format(num)) print("浮点数:{:.2f}".format(fnum)) print("字符串:{}".format(name)) ``` 输出: ``` 整数:123 浮点数:3.14 字符串:John ``` 综上所述,以上是 Python 字符串格式化的详解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值