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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值