결론부터 말하면 파이썬과 씨샵은 Left와 Right를 반대로 맞춰주면 됩니다.
C# -> 문자열.PadLeft(길이, 대체문자)
Python -> 문자열.rjust(길이, 대체문자)
1. C# 공백 대체
string s1 = '일이삼';
s1 = s1.PadRight(10, ' ');
>> s1 : '일이삼 '
s1 = s1.PadLeft(10, ' ');
>> s1 : ' 일이삼'
Python 공백 대체
2. C# '0'으로 대체
string s2 = '123';
s2 = s2.PadRight(10, '0');
>> s2 : '1230000000'
s2 = s2.PadLeft(10, '0');
>> s2 : '0000000123'
Python '0'으로 대체
3. (Advanced) 가변적인 숫자(특히 금액)을 일정한 길이의 문자열로 변경해주는 함수
C#
// C++의 szFmt.Format(%%0%dd”, nSize) 기능
Protected string getStrPrice(double p, int nSize)
{
string strTemp = “{0:”;
bool bMinus = p < 0 ? ture : false;
for (int I = 0; I< (bMinus == ture? nSize -1 : nSize); i++)
{
strTemp += ‘0’;
}
strTemp += ‘}’;
return string.Format(strTemp, P);
}
Python
'개발 > 파이썬' 카테고리의 다른 글
divmod vs % (0) | 2020.10.08 |
---|---|
divmod vs % (0) | 2020.10.08 |
코드 실행 시간 측정(테스트) (0) | 2020.10.08 |
python 문자열 자르기(C# substring과 비교) (0) | 2020.07.17 |